Reputation: 83
I have the code below and I'm strugling to add values to the vector. The end goal is to itterate through a list and for each itteration add a value to 2 rows of a vector but I'm strugling to understand how to push_back to a 2d vector.
std::vector<std::vector<int> >nns;
int i = 5;
nns.push_back(i, i);
for(int i = 0; i <nns.size(); i++)
{
for(int j = 0; j < nns[i].size(); j++)
{
std::cout << nns[i][j] << std::endl;
}
}
How would I add one column to this vector? so vector[0][0] = 0 vector[1][0] = 0?
Upvotes: 1
Views: 3266
Reputation: 83
Answer provided by Algirdas Works perfectly.
#include <iostream>
#include <vector>
using namespace std;
int main() {
std::vector<std::vector<int> > nns;
int i = 5;
nns.push_back(std::vector<int>{i});
for (int i = 0; i < nns.size(); i++) {
for (int j = 0; j < nns[i].size(); j++) {
std::cout << nns[i][j] << std::endl;
}
}
}
Upvotes: 1