C++ help!

Associate
Joined
28 Jun 2005
Posts
997
Location
London
Hi all,
I'm a c++ programming noob. Got a question.

I'm trying to store int values into a vector, then putting the int vector into another vector.
(does that makes sense?)
So i'd have the main vector, and at each element it will have a vector of ints.

I think i've saved these vectors, but i'm not sure how to access them - i've looked online and not found anything.

So, yeah. Just wondering how to cout the contents of a vector within a vector :confused:

Thanks!
 
If, for example, x is a vector<vector<int> >, then x[n] is a vector<int> so can be accessed much like a normal array, just like the outer vector - x[j] would be an integer value.

To iterate over the outer vector, and then each vector in turn, outputing the int values in turn :
Code:
        vector<vector<int> > vvi;
        vector<int> vi1, vi2;

        vi1.push_back(3);
        vvi.push_back(vi1);

        vi2.push_back(4);
        vi2.push_back(5);
        vvi.push_back(vi2);

        for (vector<vector<int> >::const_iterator vvii = vvi.begin();
                vvii != vvi.end(); ++vvii) {
                for (vector<int>::const_iterator vii = vvii->begin();
                        vii != vvii->end(); ++vii) {
                                cout << *vii << endl;
                        }
                }

- would return "3 4 5", 3 being the value inside vi1 inside vvi, and 4 and 5 being the values inside vi2 inside vvi.
Hope it helps...
 
Do you need a variable number of ints or int vectors? A two-dimensional array would be much more suited if you need a fixed number of rows where each row is the same (fixed) length, or an array of linked lists if you only need a fixed number of rows.

i.e.

Code:
int[][] 2dArray = new int[10][10];
2dArray[0][0] = 0;
2dArray[0][1] = 1;
...

C++ isn't really my thing, but I know in Java you can have jagged arrays but I don't think you can do this in C++ so you'd either have to have all sub-arrays to be the same length (or simply the length of the longest one and don't use the whole thing in the others, but that could be somewhat inefficient)

In java to have a Vector of Vectors you would do:

Code:
        Vector<Vector> outer = new Vector<Vector>();();
        
        outer.add(0,new Vector<Integer>());
        
        outer.get(0).add(0,new Integer(1));
        
        System.out.println(outer.get(0).get(0));

Not entirely sure how that'd translate to C++ but hopefully something I've said will be even remotely useful :p
 
Last edited:
Hi guys,
Thank you v.much for your replies.. I'm still not entirely getting it >.<

So... sorry to be a noob but:

"vector<vector<int> > vvi; // makes a vector of ints inside the vector
vector<int> vi1, vi2; // makes 2 more vectors, vi1 and vi2?

vi1.push_back(3); // puts 3 into vi1
vvi.push_back(vi1); // update/puts vi1 into vvi?

vi2.push_back(4); // puts 4 into vi2
vi2.push_back(5); // puts 5 into vi2
vvi.push_back(vi2);" // updates/puts vi2 into vvi?

Is there a way to make it dynamic? (i'm not sure if that is the correct term)
If data was entered...
1 2 - in one vector
3 4 - in another vector
So each line is a new vector of ints.. would it be possible to do that?

Thanks again for your time :)
 
vvi.push_back (vi1) copies vi1 to a new element of vvi (i.e. internally it calls the copy constructor of vector<int>). What do you mean by "dynamic"? This is dynamic in the sense the vectors can grow/shrink.

Tip when working with lots of STL: use typedefs to avoid typing those long horrible template names lots of times:

typedef vector<int> intvec;
typedef vector<intvec> intvecvec;
 
I mean dynamic in the sense of:
without declaring the vectors at the top, would it be possible for it to add a vector of ints from an input?
So if the input was:
1 2
3 4
5 6
So for each line of input it would create a new vector of ints inside the main vector? I'm just wondering if it would be possible to do that? I'm not sure how it would work, and can't seem to find anything about it online or in any c++ books i have.

Thanks for the typedef tip!
 
Back
Top Bottom