-1

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;
}
  • 3
    Since you created your vector vec with n elements already, push_back will add more elements after those n first elements. So, either change "vec(n);" to "vec;" or use the way in the comment "vec[i]" instead of vec.push_back(). – Steschu Sep 10 '20 at 12:55
  • Hello, and welcome to stack overflow! I'm having some trouble understanding your question. Can you rework the question to specifically address A: What you expect, B: what you get and C: what you have tried? You are more likely to get help if you can specifically articulate what the problem is in this way. Also consider reading https://stackoverflow.com/help/how-to-ask, it has some great tips to help us help you! – Native Coder Sep 10 '20 at 19:45
  • 1
    @NativeCoder: Hello! Thank you for your comment. I have edited the question that may improve your understanding of the question. Please let me know if there is anything to add to make it even better :) – AMIT PRASAD Sep 11 '20 at 05:42
  • @AMITPRASAD, Perhaps a better title for the question would be "What is the difference between vector.push_back(VALUE) and vector[INDEX]=VALUE?". I think this will help other understand where you are needing help. We like to avoid question such as "Why doesn't this work" because the question does not directly address an issue. As I read the question, I'm still not sure what you expect. When you say "this works here", what does "works" mean? What is the expected behavior? Not just "I expect them to be equivalent", but what do you expect to happen specifically. – Native Coder Sep 11 '20 at 15:25

1 Answers1

1

You create a vector with n elements here:

vector<vector<pair<int,int>>> vec(n);

If you push n more elements to that vector then it has 2*n elements where the first n elements are still the ones that were present after construction and the last n elements are the ones you pushed.

You just discovered one of the big advantages of not "manually" writing loops. You would have seen what is going on if you had written

for(const auto& row : vec) {
    for(cons auto& entry : row) {
        std::cout<<entry.first<<" ";
    }
}

In general keeping track of the size of a container separately from the the container is a very bad idea. Use .size() if you need to know its size.

Last but not least, using a debugger you could have observed what is going on in your code.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185