Home > Back-end >  Why I can't insert the pair value like this? v.push_back(vector<pair<int,string> >(
Why I can't insert the pair value like this? v.push_back(vector<pair<int,string> >(

Time:12-24

I have written this code and want to insert value in the vector of vector like this : v.push_back(vector<pair<int,string> >({1,"A"})); ??
But it is showing error ....

Inserting an empty value is working in this code. But I want to know why this is not possible ??

#include<bits/stdc  .h>
using namespace std;

void printVec(vector<pair<int,string> > &v){
    cout << "Size: " << v.size() << endl;
    for(int i=0; i<v.size(); i  )
    {
        cout << v[i].first << " " << v[i].second << endl;
    }
}

int main()
{
    vector<vector<pair<int,string> > > v;

    int N;
    cin >> N;
    for(int i=0; i<N; i  )
    {
        int n;
        cin >> n;
        v.push_back(vector<pair<int,string> >({}));
        for(int j=0; j<n; j  )
        {
            int x;
            string s;
            cin >> x >>s;
            v[i].push_back({x,s});
        }
    }

    v.push_back(vector<pair<int,string> >({})); // UNABLE TO STORE A PAIR AT THE END. ONLY CAN ADD EMPTY PAIR. DON'T KNOW WHY??
    // v.push_back({}); // USING ANY OF THEM

    for(int i=0; i<v.size(); i  )
    {
        printVec(v[i]);
    }
}

I want to insert a value at the end of a vector of vector of pair<int, string> type. Using this:- v.push_back(vector<pair<int,string> >({1,"A"}));

But it showing error.

When using this:-

v.push_back(vector<pair<int,string> >({}));

it working fine. Please explain why??

CodePudding user response:

Here you are pushing a pair instead of a vector of pairs.

v.push_back(vector<pair<int,string> >({1,"A"}));

This works because it considers ({}) as an empty vector of pairs. `

v.push_back(vector<pair<int,string> >({}));`

Although the following will work:

vector<pair<int,string> > temp;
temp.push_back({1, "Hi"});
v.push_back(vector<pair<int,string> >({temp}));

CodePudding user response:

If you want to list-initialize your vector with the {1,"A"} as the initializer for one element of the std::initializer_list constructor you need to use braces, not parentheses:

vector<pair<int,string>>{{1,"A"}}

Or alternatively, if you aren't sure how to initialize directly with an element in the vector or the above is not something you learned about yet, simply define a variable to temporarily hold the inner vector you want to push into v, push the pair element into it and then use that variable. Then you don't have to guess anything:

vector<pair<int,string>> inner;
inner.push_back({1,"A"});
 
v.push_back(inner); // alternatively `std::move(inner)` if `inner` is not used later

vector<pair<int,string> >({}) works because the {} argument can initialize the std::initializer_list of the std::initializer_list-constructor for std::vector directly to be empty. An additional set of braces in your original attempt would have worked the same way as well, although then the parentheses would be redundant.

  • Related