Anybody use vectors of vector with STL?

Posts: 321
Joined: 2004.10
Post: #1
Is this even possible? or even advisable? All the examples and tutorials
I've seen never seem to show such composition.

I've been playing all day, and I can get the code to compile, but
every time I try to access something it is a two step process
whereas I set an iterator to the first vector, and then another
iterator to the vector of the first iterator. I was wondering if there
was a more elegant solution? For instance:

typedef vector<Unit *> UnitVectorOfSameType;

UnitVectorOfSameType c = *IndependentUnits[CARRIERS];

Unit *u1 = c[0];

I can't seem to get something like Unit *u1 = IndependentUnits[CARRIERS][0];
to compile?

Is the syntax not intuative? Or I'm I missing something basic here?

Quote this message in a reply
Posts: 257
Joined: 2004.06
Post: #2
STL containers should be able to contain other STL containers (and I've done some pretty crazy ones sometimes like an STL vector of STL maps). Um, but yeah, it should work. What is IndependentUnits exactly? Since you're dereferencing it, I'm going to take a guess that it's a pointer to an STL vector. So, if you have one of those, then you can only access elements in a vector either by dereferencing the poinnter and using the [] operator or using the at() function.

Hm. SGI's STL page doesn't list an at() function for vectors but I'm pretty sure modern versions of the STL library have it since I've been using it for a while now.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Post: #3
It will work just fine. Just be wary:

//Note the space between the >'s - without it, the compile thinks it's the >> operator
std::vector<std::vector<int> > my2DVector;
Quote this message in a reply
Posts: 321
Joined: 2004.10
Post: #4
I see now that my first post was rather rambling. I've simplified my first
post my making what was a class just an int. Now I've made three sections
going from simplest to most complex. The first two are fine, but the last
one is where I loose it.

// A vector of vector of ints

typedef vector<int> VectorInt;

typedef vector<VectorInt> VectorVectorInt;

VectorVectorInt victorVector; // Compiles fine

victorVector[0][0] = 7;
victorVector[0][1] = 9;
victorVector[1][0] = 11;
victorVector[1][1] = 5;
victorVector[2][0] = 8;
victorVector[2][1] = 1;

//victorVector1[0][0].push_back(3); // CodeWarrior returns 'expression syntax error'

int Ant = victorVector[1][0]; // OK. Ant == 11

int Bad = victorVector[3][3]; // OK. Bad is garbage


// A vector of vector of pointers to ints

typedef vector<int *> VectorIntPtr;

typedef vector<VectorIntPtr> VectorVectorIntPtr;

VectorVectorIntPtr victorVectorPtr; // Compiles fine

victorVectorPtr[0][0] = &Ant;

int *ptrInt = victorVectorPtr[0][0]; // OK. ptrInt points to a value of 11


// A vector of pointers to vectors of pointers to ints

typedef vector<VectorIntPtr *> VectorVectorIntPtrPtr;

VectorVectorIntPtrPtr victorVectorPtrPtr; // Compiles fine

int **ptrptrInt = victorVectorPtrPtr[0][0]; // Here's where all hell breaks out?

What syntax should I be using to manipulate (set and retrieve values) this
beast? I've tried all kinds of permutations but I'm stumped.

Any ideas are appreciated. Thanks
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #5
What's wrong (amongst other things) is you don't understand how vectors or pointers work Rasp

std::vector<int> vec;
vec[0] = 3;

(might) kaboom! vec[0] doesn't exist.

std::vector<int> vec;
int i = vec[0];

OK; vec.size() == 1, i == 3

std::vector<std::vector<int> > vecvec;
vecvec[0][0] = 3;

(might) kaboom! vecvec[0] doesn't exist, so vecvec[0][0] certainly doesn't.

std::vector<std::vector<int> > vecvec;
int i = vecvec[0][0];

OK, vecvec.size() == 1; vecvec[0].size() == 1, vecvec[0][0] == 3

std::vector<std::vector<int> *> vecptrvec;
vecptrvec.push_back(new std::vector<int>());
int i = (*(vecptrvec[0]))[0];

Why would you want to do this? There is no benefit. Don't bother.
Quote this message in a reply
Posts: 321
Joined: 2004.10
Post: #6
Ah, but now I know how. It was these two lines which are real gems.
I've never come across this syntax and I'd never come up with it
on my own.

vecvec.push_back( std::vector<int>() );

vecptrvec.push_back( new std::vector<int>() );

So you see, it was really quite useful afterall.
Quote this message in a reply
Post: #7
He's right though, chances are you don't actually need to do this. I've certainly never had to.
Quote this message in a reply
Posts: 321
Joined: 2004.10
Post: #8
As for vectors of vectors, here's what I was conceptually trying to do:

Say I have a game with units. Something like Strategic Command.

I wanted to store units according to main types

All Units
1st vector secondary vectors
[LAND] => tank1, tank2, army1, tank3 ... tank4...
[SEA] => carrier1, carrier2, submarine1, ...
[AIR] => fighter1, fighter2, bomber1, bomber2..

Actually, this vector of vectors contains pointers to units, so
I can put fighters and bombers etc on same vector.

Now, there maybe more than one way to skin this cat (I'm starting
to think about associative STL) but I'm stubborn and now I consider
this a personal challenge Smile Or at least a learning experience.

As for why I need the above structure, there are certain operations
which I need to apply for just the LAND, SEA, or AIR types and I
don't want to have to step thru an entire list. Here, I can just
step thru the appropiate vector.
Quote this message in a reply
Post: #9
Well for something like that, use a vector of lists. Or just use an array since you won't need to resize. The reason I say to use an std::list is that you should only need iterative access, not random access but when units are created or killed, you will want constant time insertion and deletion.
Quote this message in a reply
Posts: 321
Joined: 2004.10
Post: #10
My problem was trying to use containers of container. It was my first attempt.
If I had gone with vector of lists; or list of vectors, I'm pretty sure I'd have
had the same hair pulling experience.

I felt like I've learned a hell of alot by tackling vector of vectors. Surely,
there is something in this world where it might be useful? And if not, then
remember Maxwell's comment to Queen Victoria when she was scoffing at
the usefullness of his early experiments: "Madame, of what use is a baby"
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #11
vectors of vectors certainly is useful

vectors of pointers to vectors is almost certainly not.

that's what I meant.
Quote this message in a reply
Post: #12
Well they are useful; I just brought up an array of lists because it seems much more useful for the task you described.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  C++ vectors and typedefs... WhatMeWorry 5 6,306 Jan 16, 2007 10:09 AM
Last Post: ejtttje