Sign in to follow this  

newbie needs help with C++ vectors (or resizable 2D array)

This topic is 4760 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

Basically, I am trying to define a data type that is a vector of vectors. (A resizable 2D array.) As I understand it, a vector is a resizable array. My attempt at it looks something like this:
typedef GLfloat GLTPoint[3];

struct BlockedSolid
{
  GLTPoint3 lower_left_back;
  GLTPoint3 upper_right_frot;
};

struct BlockedSolidGroup
{
  vector<BlockedSolid> blocked_solids; 
  GLint lowest_starting_x;
};

struct TotalBlockedSpace
{
  vector<BlockedSolidGroup> all_blocked_space;
  GLint group_size;//range of x-values covered by each BlockedSolidGroup
  GLint max_length;//longest (x-length) allowed for a BlockedSolid
};



boolean CheckCoordinateBlocked(GLTVector3 test_point, TotalBlockedSpace blocked_space)
{
  GLint solid_group;
  GLint solid_group_size;
  GLint i;

  solid_group = test_point[0] / blocked_space.group_size;//find correct zone to search
  solid_group_size = blocked_space.all_blocked_space[solid_group].size();//determine size of zone to be searched
...snip
}
Anyway, when I try to complie this, I get an error on my last listed line saying that 'size' is not a member of 'BlockedSolidGroup.' But if I understand my code correctly, blocked_space.all_blocked_space[solid_group] should refer to a vector and should be able to be sized. So obviously I am missing something here. Someone told me that 2D vectors were not possible in C++. (I am using Visual C++ 6.0) He said that a corect implementation would involve pointers and would be defined something like this:
int *a, *b;
a = b;
Unfortunatly, my understanding on pointers is very shaky at best (I am trying to fix this). I know that a pointer is a data type that holds a memory location. But I am not sure how that is different from any variable, which presumably has a certain place in memory. Nor am I sure how pointers can be used to solve my original problem of a nD resizable array. Thanks for any assiatance. Curtis

Share this post


Link to post
Share on other sites
Okay, the problem is that:
blocked_space.all_blocked_space[solid_group]

Refers to the BlockedSolidGroup struct, and not the template. To access the template:

blocked_space.all_blocked_space.size();

And you really should use pointers, and they are really easy to use:

class CStuff {
public:
CStuff() {;};
};

std::vector<CStuff*> StuffList;

CStuff* Stuff1 = new CStuff();

StuffList.push_back(Stuff1);//Store into the List

std::cout << "Size: " << StuffList.size() << endl;

StuffList[0]; //To access;

Also, why are you using boolean and struct's? You are switching between C and C++. Stick to C++. Use the bool keyword, and use classes.

EDIT:

Don't forget to delete the pointers once you are done with them.

Share this post


Link to post
Share on other sites
2D vectirs are possible in C++.

std::vector< std::vector<int> > foo.

remember, there's a space between the two >'s.

To dnamically resize, you need to have something to resize it, which I usually use a temporary vector.
~~~
To answer your question...
Not sure why that isn't working...
Did you #include <vector> & using namespace std;? That's my random and probably pointless guess.

Share this post


Link to post
Share on other sites
Quote:
But if I understand my code correctly, blocked_space.all_blocked_space[solid_group] should refer to a vector and should be able to be sized.


No, it is referering to a BlockedSolidGroup which does contain a vector<BlockedSolid>. You will have to use blocked_space.all_blocked_space[solid_group].blocked_solids to get to the vector.

Quote:
Someone told me that 2D vectors were not possible in C++.


You can make a vector of vectors like this: vector<vector<BlockedSolid> >. Note the space between the two > >. It is necessary to prevent the compiler from parsing it as a single >> operator (that's one of C++'s flaws). It will not really be a 'true' 2D vector, since each individual vector can be resized independently.

You could, however, write your own 2D vector class (or use an existing one, like boost::multi_array or blitz::Array).

A minimalistic 2D vector class could look like:


template<class T>
class vector2d
{
size_t m_rows, m_cols;
std::vector<T> m_data;
public:
vector2d() {}

vector2d(size_t rows, size_t cols)
: m_rows(rows), m_cols(cols), m_data(rows*cols)
{}

T& operator()(size_t row, size_t col)
{
return m_data[row*m_cols+col];
}

const T& operator()(size_t row, size_t col) const
{
return data[row*m_cols+col];
}

void resize(size_t rows, size_t cols)
{
m_data.resize(rows*cols);
m_rows = rows;
m_cols = cols;
}
};


Of course, such an implementation is far from being a real STL container. Also, it is indexed as vec(x,y) and not vec[x][y], though the later syntax could be possible in a more complex class (returning a proxy row object when you use []).

Share this post


Link to post
Share on other sites
Quote:
Original post by Quasius

solid_group_size = blocked_space.all_blocked_space[solid_group].size();//determine size of zone to be searched


You're addressing a BlockedSolidGroup here, not the vector. Dependant on which vector you want to address, you either need to remove the index operation, or select the vector in the BlockedSolidGroup.

Quote:

Someone told me that 2D vectors were not possible in C++. (I am using Visual C++ 6.0)

He lied.
std::vector<std::vector<T> >
Quote:

He said that a corect implementation would involve pointers and would be defined something like this: ...

For large objects which will be moved around frequently, pointers can be more efficient. For what you are doing (or looks like you are doing) a vector is perfectly fine.

Quote:

Also, why are you using boolean and struct's? You are switching between C and C++. Stick to C++. Use the bool keyword, and use classes.

Structures are a feature of C++, not just C. In fact, struct's are exactly the same except that they default to public access.

Share this post


Link to post
Share on other sites

This topic is 4760 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this