SOLVED, C++ vector of vectors hell

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

Recommended Posts

I've done something quite clever with vectors (created a vector of vectors) but I'm a bit out of my depth, and don't know how to pass one of the inner vectors to another object. In fact, I'm not 100% sure I've created my vectors correctly. I have a sequence of values called a Cycle, which is used for animation. These values are read in from a text file. The text file currently contains this: 2 3 4 5 4 3 2 6 7 8 7 6 -1 2 11 12 13 12 11 -1 5 6 5 6 -1 This is a list of three cycles, terminated with -1. They are read in and stored in the World object like this, in a loop which knows that there are three cycles:
//in World.h
std::vector<int> m_Values;
std::vector< std::vector<int> > m_Cycles;

//in World.cpp
int data1;
file >> data1;
while( data1 != -1 )
{
m_Values.push_back(data1);
file >> data1;
}
m_Cycles.push_back(m_Values);
m_Values.clear();


Before I ask for help with passing one of the inner vectors, perhaps I should check I've got things right so far. Do I actually have three inner vectors (m_Values vectors) contained in one outer vector called m_Cycles? Or perhaps I have just deleted the data in the inner vector each time, and in fact have just one inner vector? Maybe I have to declare more than one m_Values vector in .h? Or maybe just declare the m_Cycles vector in .h and declare m_Values in the loop in .cpp? [Edited by - darenking on August 2, 2005 4:40:33 AM]

Share on other sites
Unless you loop the code you show from World.cpp three times, m_Cycles will only have one vector. Edit: Otherwise I think it works as you want it to.

Edit: Edit: When pushing m_Values into m_Cycles, what is added to m_Cycles is a copy of m_Values, so you won't need more m_Values vectors.

Share on other sites
Quote:
 Original post by darenkingDo I actually have three inner vectors (m_Values vectors) contained in one outer vector called m_Cycles? Or perhaps I have just deleted the data in the inner vector each time, and in fact have just one inner vector?

When you push an object into a vector its value is copied, which means the answer to your first question is that every time you push m_Values into m_Cycles m_Cycles stores a copy of the m_Values you passed. So you aren't removing any data from inside m_Cycles when you change the values in m_Values after its been copyed to m_Cycles.
Quote:
 Original post by darenkingMaybe I have to declare more than one m_Values vector in .h? Or maybe just declare the m_Cycles vector in .h and declare m_Values in the loop in .cpp?

declaring m_Cycles in the header file is enough. Then to access one of the pushed vectors you either use m_Cycles[1 (or whatever)] or iterate through them. If you want to get on of the values you can just do m_Cycles[1][1]. Essentially you've made a 2d array out of std::vectors.

Hope that helps.

Share on other sites
OK, I've got that working. Thanks for that.

Now my next problem is that I need to be able to pass a pointer or reference to one of these three inner vectors to another object, my Character object (sprites, essentially). The reason I want to pass by pointer or reference rather than by copy is that I may have hundreds of Characters and don't want to waste lots of memory.

How do I access one of the inner vectors so that I am able to pass it?

And how do I store it the other end?

Share on other sites
To get a pointer to one of the inner vectors you can just do the following:
std::vector<int>* MyPointer = &m_Cycles[x];

Share on other sites
OK, how do I pass this pointer to another object?

Can I just pass it as basically a memory location, as though it were a pointer to an int rather than a pointer to a vector of ints? That way, I would surely be able to access the integers with squared brackets [] as when you access an array.

If so, what's the syntax? That's the really tricky bit for me, even if I get the concept I can never do the syntax.

Share on other sites
Quote:
 Original post by darenkingOK, how do I pass this pointer to another object?Can I just pass it as basically a memory location, as though it were a pointer to an int rather than a pointer to a vector of ints? That way, I would surely be able to access the integers with squared brackets [] as when you access an array.

You can pass it as a vector (as it is) and still be able to access it with square brackets. Just define your function or method to take a vector of that type (the one you want to pass).

If it's the inner one, then you can just index the outer one when you're passing it.

Share on other sites
Note the return type of the subscript operator on the first dimension is of type (const) std::vector<int>&.

i haven't read the entire thread but use a reference or constant reference instead otherwise you'll get confused on the syntax with pointers i have seen it happen many times [grin], just remember with either pointer/reference it has the same issues as pointer/reference to any other type.

Share on other sites
Quote:
Original post by Lotus
Quote:
 Original post by darenkingOK, how do I pass this pointer to another object?

You can pass it as a vector (as it is) and still be able to access it with square brackets.

See what i mean [grin], if you have pointer to std::vector to use its subscript operator you first need to dereference the pointer i.e. (*vec_ptr)[index] not vec_ptr[index] which is also compilable but wrong logic/behaviour etc.

Share on other sites
example -

#include <iostream>#include <vector>#include <iterator>#include <algorithm>using namespace std;void printVector(vector<int>&rvec){    copy(rvec.begin(),rvec.end(),ostream_iterator<int>(cout,"\n")); }int main(){        vector< vector<int> > vec;    vector<int>veci;        for(int x =0; x < 10; x++){        veci.push_back(x);    }    vec.push_back(veci);    veci.clear();          for(int x =10; x < 20; x++){        veci.push_back(x);    }        vec.push_back(veci);    veci.clear();        for(int x =20; x < 30; x++){        veci.push_back(x);    }        vec.push_back(veci);    veci.clear();          for(int x = 30; x < 40; x++){        veci.push_back(x);    }        vec.push_back(veci);    veci.clear();            vector<int> *pVec = &vec[1];    printVector(*pVec);        pVec = &vec[2];    printVector(*pVec);    }

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• Forum Statistics

• Total Topics
633298
• Total Posts
3011253
• Who's Online (See full list)

There are no registered users currently online

×