• Advertisement
Sign in to follow this  

Quick multi dimensional vector question

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I decided to convert all my arrays to vectors (thanks again to all who gave their input in the old thread!). Im setting things up like this...
std::vector<std::vector<int> > terObj ( 16, std::vector<int> ( 16 ) ); //terObj[16][16]

This way, I dont have to re write any code, because the vectors are accessed in the exact same way that my arrays are ( object = terObj[x][z], ect..). Works great for all my 2d arrays, because now the compiler will crash if I go out of bounds. However, im having a little trouble wrting a 3d (or more) vector. The following doesnt work...
std::vector<std::vector<std::vector<int> > > terObj ( 16, 16, std::vector<int> ( 2 ) ); //terObj[16][16][2]

The goal there is to create the same 2d array as show above, but add [2] positions at the end (to hold two objects instead of one). What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
It should be:
std::vector< std::vector< std::vector<int> > > terObj(
16,
std::vector< std::vector<int> >(
16,
std::vector<int>(2)
)
);
Or something close to that (this sort of syntax is easy to mess up). Some typedef's might make it a little easier to manage.

It's certainly convenient not to have to change your existing code, but nevertheless there might be better alternatives to the above (I'm guessing Boost's multi arrays were probably mentioned in your other thread).

Share this post


Link to post
Share on other sites
Cool that works great! Yeah many people suggested boost array, but this seems to be working fine for now. The declaration is a bit ugly, but that only happens once, and then I get a no fuss no muss array WITH bounds checking.

Share this post


Link to post
Share on other sites
Whoa! These multi dimensional vectors are SLOW in debug mode (about 10x as slow as regular arrays). Maybe its time I looked into boost array... I went to their webpage and downloaded all the "stuff", threw it into a header...



//boostArray.h

#ifndef Array_T

namespace boost {
template<typename T, std::size_t N> class array;
template<typename T, std::size_t N> void swap(array<T, N>&, array<T, N>&);
template<typename T, std::size_t N>
bool operator==(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator!=(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator<(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator>(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator<=(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator>=(const array<T, N>&, const array<T, N>&);
}

template<typename T, std::size_t N>
class array {
public:
// types
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

// static constants
static const size_type static_size = N;

// construct/copy/destruct
template<typename U> array& operator=(const array<U, N>&);

// iterator support
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;

// reverse iterator support
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;

// capacity
size_type size();
bool empty();
size_type max_size();

// element access
reference operator[](size_type);
const_reference operator[](size_type) const;
reference at(size_type);
const_reference at(size_type) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
const T* data() const;
T* c_array();

// modifiers
void swap(array<T, N>&);
void assign(const T&);

T elems[N];
};

// specialized algorithms
template<typename T, std::size_t N> void swap(array<T, N>&, array<T, N>&);

// comparisons
template<typename T, std::size_t N>
bool operator==(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator!=(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator<(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator>(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator<=(const array<T, N>&, const array<T, N>&);
template<typename T, std::size_t N>
bool operator>=(const array<T, N>&, const array<T, N>&);

#define Array_T
#endif




But everytime I try and declare an array I get "error C2079: 'testArray' uses undefined class 'boost::array<T,N>'"

I guess im missing something about how to use 'templates'?

Share this post


Link to post
Share on other sites
All you did was ripping the header apart and copying the method signatures, resulting in your header missing the actual implementation. Just include the boost headers as-is and don't try to just steal some of their code.

Share this post


Link to post
Share on other sites
As much as I love "stealing" other peoples code, that wasnt my intention here. I looked for some kind of downloadable link, but found nothing. In the entire 'array' section there are only two pages with actual code

http://www.boost.org/doc/html/array/reference.html#header.boost.array.hpp

and

http://www.boost.org/doc/html/boost/array.html

They both just scream "steal me". So where am I supposed to find the "boost headers as-is"?

Akk you dont mean I have to install the ENTIRE boost library just to use arrays?

Share this post


Link to post
Share on other sites
Zealous,

You arent getting any bounds checking when you access your vector of vectors using [x][y]. In debug mode, you will probably crash, because memory often gets initialised to obviously bad values like 0xcdcdcdcd, but in release mode, this doesnt happen, so you will get all sorts of undefined behaviour (anything from crashes to very unpredictable bugs).

To get bounds checking, you need to use .at(x).at(y)

Alex

Share this post


Link to post
Share on other sites
oooh thats good to know. I decided not to use em anyway, they are wayyy too slow in debug mode. Im hoping boost array will be better.

But I still cant find any downloadable files for the array class. Do I need to install the whole library? Its like a 9 meg download.. seems like a lot of stuff when all I want are these arrays..

Share this post


Link to post
Share on other sites
Quote:
Original post by ZealousEngine
They both just scream "steal me". So where am I supposed to find the "boost headers as-is"?

Akk you dont mean I have to install the ENTIRE boost library just to use arrays?

Actually, you can just use the headers you need, e.g.:
array.hpp - not just the synopsis, the whole header (straight from the website you linked, btw.)

/* The following code declares class array,
* an STL container (as wrapper) for arrays of constant size.
*
* See
* http://www.josuttis.com/cppcode
* for details and the latest version.
* See
* http://www.boost.org/libs/array for Documentation.
* for documentation.
*
* (C) Copyright Nicolai M. Josuttis 2001.
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
* 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
* 05 Aug 2001 - minor update (Nico Josuttis)
* 20 Jan 2001 - STLport fix (Beman Dawes)
* 29 Sep 2000 - Initial Revision (Nico Josuttis)
*
* Jan 29, 2004
*/

#ifndef BOOST_ARRAY_HPP
#define BOOST_ARRAY_HPP

#include <cstddef>
#include <stdexcept>
#include <boost/assert.hpp>

// Handles broken standard libraries better than <iterator>
#include <boost/detail/iterator.hpp>
#include <algorithm>

// FIXES for broken compilers
#include <boost/config.hpp>


namespace boost {

template<class T, std::size_t N>
class array {
public:
T elems[N]; // fixed-size array of elements of type T

public:
// type definitions
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

// iterator support
iterator begin() { return elems; }
const_iterator begin() const { return elems; }
iterator end() { return elems+N; }
const_iterator end() const { return elems+N; }

// reverse iterator support
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
// workaround for broken reverse_iterator in VC7
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
reference, iterator, reference> > reverse_iterator;
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
const_reference, iterator, reference> > const_reverse_iterator;
#else
// workaround for broken reverse_iterator implementations
typedef std::reverse_iterator<iterator,T> reverse_iterator;
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
#endif

reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}

// operator[]
reference operator[](size_type i)
{
BOOST_ASSERT( i < N && "out of range" );
return elems;
}

const_reference operator[](size_type i) const
{
BOOST_ASSERT( i < N && "out of range" );
return elems;
}

// at() with range check
reference at(size_type i) { rangecheck(i); return elems; }
const_reference at(size_type i) const { rangecheck(i); return elems; }

// front() and back()
reference front()
{
return elems[0];
}

const_reference front() const
{
return elems[0];
}

reference back()
{
return elems[N-1];
}

const_reference back() const
{
return elems[N-1];
}

// size is constant
static size_type size() { return N; }
static bool empty() { return false; }
static size_type max_size() { return N; }
enum { static_size = N };

// swap (note: linear complexity)
void swap (array<T,N>& y) {
std::swap_ranges(begin(),end(),y.begin());
}

// direct access to data (read-only)
const T* data() const { return elems; }

// use array as C array (direct read/write access to data)
T* c_array() { return elems; }

// assignment with type conversion
template <typename T2>
array<T,N>& operator= (const array<T2,N>& rhs) {
std::copy(rhs.begin(),rhs.end(), begin());
return *this;
}

// assign one value to all elements
void assign (const T& value)
{
std::fill_n(begin(),size(),value);
}

// check range (may be private because it is static)
static void rangecheck (size_type i) {
if (i >= size()) {
throw std::range_error("array<>: index out of range");
}
}

};

// comparisons
template<class T, std::size_t N>
bool operator== (const array<T,N>& x, const array<T,N>& y) {
return std::equal(x.begin(), x.end(), y.begin());
}
template<class T, std::size_t N>
bool operator< (const array<T,N>& x, const array<T,N>& y) {
return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
}
template<class T, std::size_t N>
bool operator!= (const array<T,N>& x, const array<T,N>& y) {
return !(x==y);
}
template<class T, std::size_t N>
bool operator> (const array<T,N>& x, const array<T,N>& y) {
return y<x;
}
template<class T, std::size_t N>
bool operator<= (const array<T,N>& x, const array<T,N>& y) {
return !(y<x);
}
template<class T, std::size_t N>
bool operator>= (const array<T,N>& x, const array<T,N>& y) {
return !(x<y);
}

// global swap()
template<class T, std::size_t N>
inline void swap (array<T,N>& x, array<T,N>& y) {
x.swap(y);
}

} /* namespace boost */
#endif /*BOOST_ARRAY_HPP*/


As you can see, you need some other boost headers as well, so it's best just to download the package. You don't actually need to "install" anything apart from unpacking it somewhere and add the path to your IDE's default include directories.
Compilation is only required if yo want to use system-specific functionality like boost.filesystem and a couple of others. Most things, however, are just header files that can be included without adding any dependencies outside the STL.

HTH,
Pat.

Share this post


Link to post
Share on other sites
Hmm has anyone had trouble extracting those boost exe's? Ive tryed both 1.33 and the latest version, but they hang when im trying to extract them. Ive tryed downloading from multiple mirrors and get the same problem... what the heck..

Share this post


Link to post
Share on other sites
This is INSANE! I thought it had to be something on my end, so I defragged, ran a complete virus scan, ect... Tryed again today, downloaded the latest boost (1.33.1 I think), and it just hangs while extracting at exactly 43% (stupid self extracting exes! give me a damn rar!). Ive tryed some of the older versions, from different mirrors, and they all have trouble extracting.

Last night I let one version extract for about 15+ min, and it did eventually finish (final size was a whopping 20meg). However when I went to browse the folder, my computer kept hanging. When I went to trash the folder, it also took about 15min.

I really doubt its a problem with my computer, as I have NEVER had a problem like this since I built the machine 2+ years ago. Ill try and send the boost people an email, but I dont see how they can help. Long story short, I guess God doesnt want me to use Boost.

If anyone is in a good mood, could you please RAR these damn files? Or point me to someplace else where I can get them?

Share this post


Link to post
Share on other sites
Where is there a zip? Looking now, all i found was the exe..

*edit Ah cool! I musta skimmed over it because it said "Platform-Independent". I guess it should work on xp eh? Wohoo its extracting now!!! Winrar FTW!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement