Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vector of a vector

Status
Not open for further replies.

cppdev

Programmer
May 12, 2003
23
US
Howdy,
I am creating a dynamic 2D data structure. I am using vectors (unless i find a better option). I am also new to the STL.
I also am using the personal edition of C++ Builder 6.

How can i declare and manipulate a vector of a vector?

can i declare a vector of a vector like this ?

vector <vector<ElemType>> Element;

How would i manipulate this list? especially, how can i specify to add/remove Element on Row X, Column Y.
I am not supposed to have memory reserved for every row, just where it is needed. We are looking for a reasonably efficent way of doing this, but we need to use the STL.

this might be an example of the data structure, using E as a vector element:


E-E-E
|
E
|
E-E
|
E-E-E-E-E-E-E
|
E-E-E-E


Thanks In Advance,
Tom
 
I think you would create there a tree structure. You can create like it this:

class xxx
{
....
some data there
...
vector<xxx> children;
....
some other data there
...
};


Ion Filipski
1c.bmp
 
> tree strcuture

Maybe, though it doesn't look like he really needs lists hanging off of any elements except the first, so a 2D array might make more sense.


Your syntax for vectors of vectors is correct, though remember to put a space between your right-angle-brackets so it doesn't look like operator >> to the compiler.

You can think of my_vector[ 5 ][ 300 ] as the element in the fifth row of the 300th column.

Possibly preferable to a vector of vectors is Boost's ( multidimensional arrays. They're STL-compliant, but not Standard STL. Depending on what you're doing, they might be useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top