Pushing back a vector into a vector of vectors and then printing out the vector

Lets say you have


1
2
3
4
5

 std::vector<int> tmp{ 1,1,1,1}

 std::vector<std::vector<int>> tmp2;




How would you be able to store the tmp vector into tmp2 vector? can you just use tmp2.push_back(tmp) ?

And if I wanted to print out the vector tmp, How would I do so?

Many thanks.
Last edited on
Can you just use tmp2.push_back(tmp)

Yes

If you wanted to retrieve the tmp vector from the tmp2 vector how would you do this?

Say tmp2[0].
Thank you. Would you not also need to refer to a column such as tmp2[0][0]?

I have stored the vector as described above however when I tried printing out the vector using tmp2[0] It gives many lines of error. When I try to print it using tmp2[0][0] nothing happens.


Here is the code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>



void generate_list(std::vector<std::vector<int>> list, int length, int symbols);


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

    generate_list( list , 6, 4);

    for( int i = 0; i< list.size(); i++){
        std::cout<< list[i][0]<< std::endl;
    }

    return 0;
}




void generate_list(std::vector<std::vector<int>> list, int length, int symbols){

        int possibilities = std::pow(symbols, length);

        for (int i=0;i< possibilities; i++) {
            std::vector<int> tmp;
            std::vector<int> tmp2;


            int quotient = i;
            while (quotient!=0) {
                tmp.push_back(quotient % length);
                quotient = quotient / length;
            }


            int zerosneeded = length - tmp.size();

            while (zerosneeded != 0) {
                tmp.push_back(0);
                zerosneeded--;
            }

            for( int i = tmp.size(); i >=0; i--){
                tmp2.push_back(tmp[i]);
            }

            list.push_back(tmp2);

    }
}

Last edited on
closed account (E0p9LyTq)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>

int main()
{
   // create a 2D vector with known sizes
   unsigned number_rows = 4;
   unsigned number_cols = 3;

   // this creates a 2D vector filled with zeroes
   std::vector<std::vector<int>> aVec(number_rows, std::vector<int>(number_cols));

   std::cout << "Printing out a 2D vector:\n";

   for (unsigned i = 0; i < aVec.size(); i++)
   {
      for (unsigned j = 0; j < aVec[i].size(); j++)
      {
         std::cout << aVec[i][j] << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // create a 2D vector with user supplied values.
   std::cout << "Number of rows: ";
   std::cin >> number_rows;
   std::cout << "Number of columns: ";
   std::cin >> number_cols;

   // this also creates a 2D vector filled with zeroes.
   std::vector<std::vector<int>> aVec2(number_rows, std::vector<int>(number_cols));

   std::cout << "\nPrinting out another 2D vector:\n";

   for (const auto& i : aVec2)
   {
      for (const auto& j : i)
      {
         std::cout << j << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // create an emptry 2D vector
   std::vector<std::vector<int>> aVec3;

   std::cout << "Size of empty 2D vector: " << aVec3.size() << "\n\n";

   // get user supplied values for rows and columns
   std::cout << "Number of rows: ";
   std::cin >> number_rows;
   std::cout << "Number of columns: ";
   std::cin >> number_cols;

   // fill the 2D vector, loop through each row
   for (unsigned i = 0; i < number_rows; i++)
   {
      // create a temp vector holding all the column values for each row
      std::vector<int> temp_row;

      // loop through each column
      for (unsigned j = 0; j < number_cols; j++)
      {
         // push back a value for each column in the row
         temp_row.push_back(((100 * (i + 1)) + j + 1));
      }

      // each row vector is complete, let's push it back.
      aVec3.push_back(temp_row);
   }

   std::cout << "\nPrinting out filled empty 2D vector:\n";

   for (const auto& i : aVec3)
   {
      for (const auto& j : i)
      {
         std::cout << j << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';
}
Printing out a 2D vector:
0 0 0
0 0 0
0 0 0
0 0 0

Number of rows: 3
Number of columns: 4

Printing out another 2D vector:
0 0 0 0
0 0 0 0
0 0 0 0

Size of empty 2D vector: 0

Number of rows: 4
Number of columns: 5

Printing out filled empty 2D vector:
101 102 103 104 105
201 202 203 204 205
301 302 303 304 305
401 402 403 404 405
closed account (E0p9LyTq)
If you want to get real fancy overload std::ostream operator>> so printing a 1D or 2D vector is as easy as printing out any Plain Old Data (POD) type, like an int variable.

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

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

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

int main()
{
   unsigned number_rows = 4;
   unsigned number_cols = 3;


   // get user supplied values for rows and columns
   std::cout << "Number of rows: ";
   std::cin >> number_rows;
   std::cout << "Number of columns: ";
   std::cin >> number_cols;

   std::vector<std::vector<int>> aVec;

   // fill the 2D vector, loop through each row
   for (unsigned i = 0; i < number_rows; i++)
   {
      // create a temp vector holding all the column values for each row
      std::vector<int> temp_row;

      // loop through each column
      for (unsigned j = 0; j < number_cols; j++)
      {
         // push back a value for each column in the row
         temp_row.push_back(((100 * (i + 1)) + j + 1));
      }

      // each row vector is complete, let's push it back.
      aVec.push_back(temp_row);
   }

   // printing out a 1D or 2D vector now is really easy
   std::cout << "\nPrinting out filled empty 2D vector:\n" << aVec << '\n';
}


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

   return os;
}


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

   return os;
}
Number of rows: 4
Number of columns: 5

Printing out filled empty 2D vector:
101 102 103 104 105
201 202 203 204 205
301 302 303 304 305
401 402 403 404 405

I chose to make a generic template for the output so you can print out any type of vector. int, double, std::string or even a custom class if you have overloaded operator>> for the class.
Topic archived. No new replies allowed.