multi-dimensional vectors

I've seen code examples for assigning 2 dimensional vectors, but I haven't seen code for assigning more than that. I tried to create a 3 dimensional vector, and the only code the IDE didn't complain about was
1
2
3
4
5
int x = 2;
int y = 2;
int z = 2;
vector < vector < vector <string> > >stringvec;
stringvec.assign(x, vector <string>(y), vector <string>(z));

Would this be the correct way of producting a vector[2][2][2]?
Hi there,

Yes, your declaration of such a vector would be correct.
However, note you have used ints in your example, but the vector ultimately holds strings.

Also, i think your use of std::vector<>.assign() is incorrect: http://www.cplusplus.com/reference/vector/vector/assign/

Hope that helps.

All the best,
NwN
Edit : It seems I was way off in my understanding of vectors.

Would this method be appropriate to assigning a multi-dimensional vector of vector[1][2][3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int xvar = 1;
int yvar = 2;
int zvar = 3;
vector <string> x;
vector < vector <string> > y;
vector < vector < vector <string> > >z;
string fill = "-1";
for(int i  = 0; i < xvar; i++)
{
   x.pushback(fill);
}
for(int i = 0; i < yvar; i++)
{
   y.pushback(x);
}
for(int i = 0; i < zvar; i++)
{
   z.pushback(y);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <string>
#include <vector>
#include <iostream>

int main()
{
    {
        // make a 3D vector of rank 3 x 4 x 5
        std::vector< std::string > vec(5) ; // vector of five strings
        std::vector< std::vector< std::string > > vec_2d( 4, vec ) ; // 4 x 5
        std::vector< std::vector< std::vector< std::string > > > vec_3d( 3, vec_2d ) ; // 3 x 4 x 5

        // print sizes
        std::cout << "vec 3d size:" << vec_3d.size() << '\n' ;
        for( const auto& v2 : vec_3d )
        {
            std::cout << "    vec 2d size:" << v2.size() << "  [ " ;
            for( const auto& v : v2 ) std::cout << "vec size:" << v.size() << "  " ;
            std::cout << "]\n" ;
        }
    }

    {
        // make a 3D vector of rank 5 X 4 X 3
        std::vector< std::vector< std::vector< std::string > > > vec_3d(5) ; // 5 x 0 x 0
        for( auto& v2 : vec_3d )
        {
            v2.resize(4) ; // 5 x 4 x 0
            for( auto& v : v2 ) v.resize(3) ; // 5 x 4 x 3
        }


        // print sizes
        std::cout << "\n\nvec 3d size:" << vec_3d.size() << '\n' ;
        for( const auto& v2 : vec_3d )
        {
            std::cout << "    vec 2d size:" << v2.size() << "  [ " ;
            for( const auto& v : v2 ) std::cout << "vec size:" << v.size() << "  " ;
            std::cout << "]\n" ;
        }
    }

}

http://ideone.com/Nwmh72
edit : mistake, thanks for the info
Last edited on
> but it seems to me that all you're doing is re-sizing that particular dimension over and over again.

We need to resize each element (which is a vector) individually.

1
2
3
4
std::vector<int> array[3] ; // array of three empty vectors
array[0].resize(5) ; // row 0 resized to five, rows 1 and 2 are still empty 
array[1].resize(5) ; // row 1 resized to five, row 2 is still empty
array[2].resize(5) ; // row 2 resized to five 



> Would not :
> ...
> operate similarily?

Yes, it would (in the first example).
Topic archived. No new replies allowed.