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.
#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 (constauto& i : aVec2)
{
for (constauto& 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 (constauto& i : aVec3)
{
for (constauto& 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
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.
#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 (autoconst& x : v)
{
std::cout << x << '\n';
}
return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
for (autoconst& 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.