Why vector.push_back(s)
does not work here but vector[i]=s
works (mentioned as comment)? If I use the push_back
function, nothing gets printed. vec[i]=s
works perfectly fine, but why is the push_back
not working? I expect both the alternatives to run equally good that is print the stored elements in a 2-d vector.
#include<iostream>
#include<vector>
#include<utility>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<pair<int,int>>> vec(n);
for(int i=0;i<n;i++)
{
int m;
cin>>m;
vector<pair<int,int>> s;
for(int j=0;j<m;j++)
{
int x;
cin>>x;
s.push_back(make_pair(x,i+1));
}
vec.push_back(s);//vec[i]=s works here :-(
}
for(int i=0;i<n;i++)
for(int j=0;j<vec[i].size();j++)
cout<<vec[i][j].first<<" ";
return 0;
}