-2

I have written this code, and want to insert a value in the vector<vector> like this:

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

But it shows an error. I want to know why this is not possible.

Inserting an empty value works:

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

Here is the code:

#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]);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • "Why I can't insert the pair value like this?" - Because that's not valid C++. You need to *learn* the language, not just guess. – Jesper Juhl Dec 23 '22 at 16:27
  • Please provide me a good source of learning C++ – Amar Naskar Dec 23 '22 at 16:32
  • 1
    [https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) My advice is to pick either of the "Introductory, no previous programming experience" books and read them from cover to cover following along with the chapter exercises at the end of each chapter. – drescherjm Dec 23 '22 at 16:35
  • 1
    https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jesper Juhl Dec 23 '22 at 16:39

2 Answers2

0

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.

user17732522
  • 53,019
  • 2
  • 56
  • 105
0

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}));