简体   繁体   English

为什么我不能像这样插入对值? v.push_back(矢量<pair<int,string> &gt;({1,"A"}));? </pair<int,string>

[英]Why I can't insert the pair value like this? v.push_back(vector<pair<int,string> >({1,"A"}));?

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"}));我已经写了这段代码,想像这样在 vector of vector 中插入值: 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.我想在 pair<int, string> 类型的向量的向量末尾插入一个值。 Using this:- v.push_back(vector<pair<int,string> >({1,"A"}));使用这个:- 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??请解释为什么??

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

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:如果你想用{1,"A"}作为std::initializer_list构造函数的一个元素的初始化器来列表初始化你的向量,你需要使用大括号,而不是圆括号:

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.或者,如果您不确定如何直接使用向量中的元素进行初始化,或者您还没有学到上述内容,只需定义一个变量来临时保存您想要推入v的内部向量,推入对元素放入其中,然后使用该变量。 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. vector<pair<int,string> >({})起作用是因为{}参数可以将std::initializer_liststd::initializer_liststd::vector的构造函数直接为空。 An additional set of braces in your original attempt would have worked the same way as well, although then the parentheses would be redundant.在您最初的尝试中,一组额外的大括号也会以同样的方式工作,尽管那样括号会是多余的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 地图 <string, vector <pair<int, int> &gt;&gt;推回成对? - map<string, vector <pair<int, int> > > pushing back into pair? 我是否可以像** vector &lt;pair这样进行赋运 <int ,vector < int > &gt;&gt; Vec **在C ++中? - Can I have delcaration like ** vector < pair <int ,vector < int >> > vec** in C++? 将元素插入多图的语法 <string, vector<pair<string, int> &gt;&gt; - Syntax to insert elements into multimap<string, vector<pair<string, int>>> 推回静态向量&lt;对 <string, double[9]> &gt; myVector; - Push back to a static vector< pair<string, double[9]> > myVector; 如何对vector &lt;pair &lt;int,pair进行排序 <int , pair<string , pair<int , int > &gt;&gt;&gt;&gt;? - How to sort vector< pair< int , pair<int , pair<string , pair<int , int > > > > >? 用字符串和整数对排序矢量 - Sort Vector with string and int pair 为什么我在“push_back”对向量的第二个元素中出现 C3867 错误? - Why I have the C3867 error in `push_back` the second element of a pair vector? 如何传递向量<pair<int,int> &gt;v[] </pair<int,int> - How to pass vector<pair<int,int> >v[] 为什么 std::array 不能<std::pair<int,int> , 3&gt; 使用嵌套初始化列表进行初始化,但 std::vector <std::pair<int,int> &gt; 可以吗? </std::pair<int,int></std::pair<int,int> - Why can't std::array<std::pair<int,int>, 3> be initialized using nested initializer lists, but std::vector<std::pair<int,int>> can? 如何对矢量进行排序 <pair<string , pair<int , int> &gt;&gt;? - How to sort a vector<pair<string , pair<int , int>>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM