vectors: how create and initializate multidimensional?

Pages: 123
how we initializate a multidimensional vectors?
we can do:
vector<int> vec{3,4,5};
but we can't do these for 2D:
vector<int> vec{{3,4,5},{0,6}};
what you can say about it?
The vector is 1D.

You can emulate 2D by storing vectors in a vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>

int main()
{
  using Row = std::vector<int>;
  std::vector<Row> foo {{1,2,3}, {5,8}};
  for ( auto& row : foo ) {
    for ( auto x : row ) {
      std::cout << ' ' << x;
    }
    std::cout << '\n';
  }
}
and for 3D or more? uses the same way?
1
2
3
using vec1D= std::vector<int>;
using vec2D= vec1D;
  std::vector<vec2D> foo {{1,2,3}, {5,8}};


???
I think line 2 should be using vec2D=std::vector<vec1D>;
For true 2D vectors:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <vector>

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<std::vector<T>>&);

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<T>&);

int main()
{
   std::cout << "Creating a 2D vector with known elements:\n";

   std::vector<std::vector<int>> vec1 { { 3, 4, 5 }, { 0, 6 } };

   for (const auto& itr_row : vec1)
   {
      for (const auto& itr_col : itr_row)
      {
         std::cout << itr_col << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   std::cout << "Creating a 2D vector with known row and column sizes:\n";

   int rows { 3 };  int cols { 4 };

   std::vector<std::vector<int>> vec2(rows, std::vector<int>(cols));

   for (size_t row { }; row < vec2.size(); row++)
   {
      for (size_t col { }; col < vec2[row].size(); col++)
      {
         vec2[row][col] = (100 * (row + 1)) + col + 1;
      }
   }

   // overriding operator<< with two templated functions makes displaying a 2D vector easy
   std::cout << vec2;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
   for (auto const& x : v)
   {
      os << x << '\n';
   }

   return os;
}


template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

Creating a 2D vector with known elements:
3 4 5
0 6

Creating a 2D vector with known row and column sizes:
101 102 103 104
201 202 203 204
301 302 303 304
Last edited on
yes doug4.. thank you...
now i need understand 1 thing:
what means the '{1,2,3}, {5,8}'
ok... it's a 2D array... but the values where they are? i'm confused here
For true 3D vectors:
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
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<T>&);
template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<std::vector<T>>&);

int main()
{
   std::cout << "Creating a 3-dimensional vector\n";
   int depth { 3 };  int row { 4 };  int col { 5 };

   // create a 3 dimensional int vector with known dimensions
   std::vector<std::vector<std::vector<int>>> aVector(depth, std::vector<std::vector<int>>(row, std::vector<int>(col, 0)));

   // let's display the initial 3D vector
   std::cout << aVector << '\n';

   // initialize the vector with some values
   for (int depth_loop = 0; depth_loop < depth; depth_loop++)
   {
      for (int row_loop = 0; row_loop < row; row_loop++)
      {
         for (int col_loop = 0; col_loop < col; col_loop++)
         {
            aVector[depth_loop][row_loop][col_loop] = (((depth_loop + 1) * 100)
                                                       + ((row_loop + 1) * 10)
                                                       + col_loop + 1);
         }
      }
   }

   // let's display the filled 3D vector
   std::cout << "The filled 3D vector:\n";
   std::cout << aVector;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (const auto& x : v) { os << x << ' '; }
   return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
   for (const auto& x : v) { os << x << '\n'; }
   return os;
}

Creating a 3-dimensional vector
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0


The filled 3D vector:
111 112 113 114 115
121 122 123 124 125
131 132 133 134 135
141 142 143 144 145

211 212 213 214 215
221 222 223 224 225
231 232 233 234 235
241 242 243 244 245

311 312 313 314 315
321 322 323 324 325
331 332 333 334 335
341 342 343 344 345
Keep in mind, (always measure, but...) it may help locality and performance if the vector is kept as normal, 1D vector.
For example, when dealing with images that I send to OpenGL I always represent my images as 1D vectors. If you need to access a particular x, y cell of the "2D" data, you can do:
1
2
3
4
5
std::vector<int> my_vec(width * height);
size_t y = 3;
size_t x = 2;
size_t index = y * width + x;
my_vec[index] = 42;

Similar logic can be applied to higher dimensions.
now i get it:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    cout<<"Hello World";
    using Row = std::vector<int>;
  std::vector<Row> foo {{1,100,3}, {5,8}};
  cout << foo[0][1];
    return 0;
}

the output is '100'... thank you so much for all to all
Last edited on
To create a 3D vector with known values:

38
39
40
   std::vector<std::vector<std::vector<int>>> vec1 { { { 1, 2, 3},
                                                       { 3, 4, 5 },
                                                       { 5, 6, 7 } } };


Notice there are "extra" initializer list brackets needed for 3D.
the '{ 1, 2, 3}' can be '{ {1,3,4}, 2, 3}'?
i need some correction
i need some correction


No. { {1,3,4}, 2, 3} is mixing a vector<int> with individual int values into a a containing vector. All members of a vector must be of the same type.

Initializing 1D array:
1
2
using Vec1D = std::vector<int>
Vec1D vec1 {1, 2, 3};


Initializing 2D array:
1
2
3
4
5
using Vec2D = std::vector<Vec1D>
//Vec2D vec2 { Vec1D(), Vec1D(), Vec1D()}
Vec2D vec2 {
             {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
           };


Initializing 3D array:
1
2
3
4
5
6
7
using Vec3D = std::vector<Vec2D>
//Vec3D vec3 { Vec2D(), Vec2D(), Vec2D()}
Vec3D vec3 {
             { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
             { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} },
             { {2, 4, 6}, {4, 0, 2}, {4, 6, 8} },
           };
thank you so much for all to all
can initialize them(all elements) to zero?
If you construct a vector of known size -- std::vector<int> vec(12); -- you will get 12 elements with the default value of zero for int. A blank string ("") if the type was std::string, etc.

You can specify a fill value if you want -- std::vector<int> vec(10, 42);. 10 elements, all initialized to 42.

From one of my previous examples, creating a 2D vector:
28
29
30
   int rows { 3 };  int cols { 4 };

   std::vector<std::vector<int>> vec2(rows, std::vector<int>(cols));

All 12 elements are initialized to zero. (No value specified after the cols variable)
these line is possible?
std::fill(v.begin(), v.end(), 0);
thinking the 'v' is a multidimensional vector
these line is possible?
std::fill(v.begin(), v.end(), 0);
thinking the 'v' is a multidimensional vector

No. I Frankensteined some code to find out if it is possible.

I had an idea it wasn't, but better to actually write code for a test:

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 <iostream>
#include <vector>
#include <algorithm>

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<std::vector<T>>&);

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<T>&);

int main()
{
   std::vector<std::vector<int>> vec(3, std::vector<int>(4, 100));

   std::cout << vec << '\n';

   // is this possible for a 2D vector?
   std::fill(vec.begin(), vec.end(), 0); // NO!

   std::cout << vec << '\n';
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
   for (auto const& x : v)
   {
      os << x << '\n';
   }

   return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

Line 18 makes the compiler go all wacko and errors out.

Because we are trying to assign a number to a vector.

vec is a std::vector that contains std::vectors. Each of those contained vectors contain int values.

So I tried a loop that walks through the outer vector, the rows, and then used std::fill on the inner vectors. The vectors that hold the columns.

Success!

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
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <algorithm>

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<std::vector<T>>&);

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<T>&);

int main()
{
   std::vector<std::vector<int>> vec(3, std::vector<int>(4, 100));

   std::cout << vec << '\n';

   // is this possible for a 2D vector?
   // std::fill(vec.begin(), vec.end(), 0); // NO!

   // this is possible for a 2D vector
   for (auto& itr : vec)
   {
      std::fill(itr.begin(), itr.end(), 0);
   }

   std::cout << vec << '\n';
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<T>>& v)
{
   for (auto const& x : v)
   {
      os << x << '\n';
   }

   return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      os << x << ' ';
   }

   return os;
}

100 100 100 100
100 100 100 100
100 100 100 100

0 0 0 0
0 0 0 0
0 0 0 0

I write a lot of Frankensteined code snippets, so I can test questions on how-to-use some C++ feature. Either questions I have, or questions someone here asks.
BTW, if you are not finished asking questions in this thread I recommend you turn OFF the "solved topic" status.
heres my sample code that works:
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
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //creating a 2D vector:
    vector<vector<int>> vec;
    
    //creating a 3D vector:
    //vector < vector < vector < int > > > vec;
    //the number of dimensions is the number of 'vector' and '<'\'>'
    
    
    //resize the 1st dimension:
    vec.resize(20);//we must resize the 1st dimension for we start
    
    //now resize the 2nd dimension from the 1st(yes... 1st dimension all elements)
    for(unsigned int i=0 ; i<20; i++)
    {
       vec[i].resize(20); 
    }
    //if we have more dimensions, we make the same...
    
    
    //change all 'vec' elements to '10':
    for (auto& itr : vec)
    {
        std::fill(itr.begin(), itr.end(), 10);
    }   
    
    //get 1 element out put:
    cout << vec[1][19];
    
    
    return 0;
}

now i know what i need ;)
thank you so much for all
You can have more compact code if you combine the for loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>

int main()
{
   std::vector<std::vector<int>> vec;

   vec.resize(20);

   for (auto& itr : vec)
   {
      itr.resize(20);

      std::fill(itr.begin(), itr.end(), 10);
   }

   std::cout << vec[1][19] << '\n';
}

a 3D vector is one more for loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

int main()
{
   std::vector<std::vector<std::vector<int>>> vec3d;

   vec3d.resize(10);

   for (auto& itr1 : vec3d)
   {
      itr1.resize(10);

      for (auto& itr2 : itr1)
      {
         itr2.resize(10);

         std::fill(itr2.begin(), itr2.end(), 25);
      }
   }

   std::cout << vec3d[3][4][5] << '\n';
}
Pages: 123