Assigning to a two dimensional vector in c++

problem_solver picture problem_solver · Jul 21, 2017 · Viewed 9.7k times · Source
#include<bits/stdc++.h>
using namespace std;
main()
{
    vector<vector<int> > v;
    for(int i = 0;i < 3;i++)
    {
        vector<int> temp;
        for(int j = 0;j < 3;j++)
        {
            temp.push_back(j);
        }
        //cout<<typeid(temp).name()<<endl;
        v[i].push_back(temp);
    }
 }

I am trying to assign to a two dimensional vector. I am getting the following error

No matching function for call to 
std ::vector<int>::push_back(std::vector<int> &)

Answer

Itban Saeed picture Itban Saeed · Jul 21, 2017

Problem: Your vector v is empty yet and you can't access v[i] without pushing any vector in v.

Solution: Replace the statement v[i].push_back(temp); with v.push_back(temp);