加上一对向量

时间:2022-05-01 04:22:19

I have a vector of pair like such:

我有一对这样的向量:

vector<pair<string,double>> revenue;

I want to add a string and a double from a map like this:

我想从这样的地图上添加一个字符串和一个double:

revenue[i].first = "string";
revenue[i].second = map[i].second;

But since revenue isn't initialized, it comes up with an out of bounds error. So I tried using vector::push_back like this:

但是由于收入没有初始化,它会出现超出界限的错误。所以我尝试使用vector::push_back:

revenue.push_back("string",map[i].second);

But that says cannot take two arguments. So how can I add to this vector of pair?

但这句话不能有两个论点。那么我怎么才能把这个对的向量相加呢?

7 个解决方案

#1


79  

Use std::make_pair:

使用std::make_pair:

revenue.push_back(std::make_pair("string",map[i].second));

#2


20  

IMHO, a very nice solution is to use c++11 emplace_back function:

IMHO,一个很好的解决方案是使用c++11 emplace_back函数:

revenue.emplace_back("string", map[i].second);

It just creates a new element in place.

它只是在适当的地方创建一个新元素。

#3


9  

revenue.pushback("string",map[i].second);

But that says cannot take two arguments. So how can I add to this vector pair?

但这句话不能有两个论点。那么我怎么才能加上这个向量对呢?

You're on the right path, but think about it; what does your vector hold? It certainly doesn't hold a string and an int in one position, it holds a Pair. So...

你走的路是对的,但是想想看;向量包含什么?它当然不会把一个字符串和一个int放在一个位置,它会保存一对。所以…

revenue.push_back( std::make_pair( "string", map[i].second ) );     

#4


5  

Read the following documentation:

阅读以下文档:

http://cplusplus.com/reference/std/utility/make_pair/

http://cplusplus.com/reference/std/utility/make_pair/

or

http://en.cppreference.com/w/cpp/utility/pair/make_pair

http://en.cppreference.com/w/cpp/utility/pair/make_pair

I think that will help. Those sites are good resources for C++, though the latter seems to be the preferred reference these days.

我想这会有帮助。这些站点对于c++来说是很好的资源,尽管后者似乎是当前的首选参考。

#5


4  

Or you can use initialize list:

也可以使用initialize list:

revenue.push_back({"string", map[i].second});

#6


1  

revenue.push_back(pair<string,double> ("String",map[i].second));

this will work.

这将工作。

#7


-2  

Try using another temporary pair:

尝试使用另一种临时配对:

pair<string,double> temp;
vector<pair<string,double>> revenue;

// Inside the loop
temp.first = "string";
temp.second = map[i].second;
revenue[i].push_back(temp);

#1


79  

Use std::make_pair:

使用std::make_pair:

revenue.push_back(std::make_pair("string",map[i].second));

#2


20  

IMHO, a very nice solution is to use c++11 emplace_back function:

IMHO,一个很好的解决方案是使用c++11 emplace_back函数:

revenue.emplace_back("string", map[i].second);

It just creates a new element in place.

它只是在适当的地方创建一个新元素。

#3


9  

revenue.pushback("string",map[i].second);

But that says cannot take two arguments. So how can I add to this vector pair?

但这句话不能有两个论点。那么我怎么才能加上这个向量对呢?

You're on the right path, but think about it; what does your vector hold? It certainly doesn't hold a string and an int in one position, it holds a Pair. So...

你走的路是对的,但是想想看;向量包含什么?它当然不会把一个字符串和一个int放在一个位置,它会保存一对。所以…

revenue.push_back( std::make_pair( "string", map[i].second ) );     

#4


5  

Read the following documentation:

阅读以下文档:

http://cplusplus.com/reference/std/utility/make_pair/

http://cplusplus.com/reference/std/utility/make_pair/

or

http://en.cppreference.com/w/cpp/utility/pair/make_pair

http://en.cppreference.com/w/cpp/utility/pair/make_pair

I think that will help. Those sites are good resources for C++, though the latter seems to be the preferred reference these days.

我想这会有帮助。这些站点对于c++来说是很好的资源,尽管后者似乎是当前的首选参考。

#5


4  

Or you can use initialize list:

也可以使用initialize list:

revenue.push_back({"string", map[i].second});

#6


1  

revenue.push_back(pair<string,double> ("String",map[i].second));

this will work.

这将工作。

#7


-2  

Try using another temporary pair:

尝试使用另一种临时配对:

pair<string,double> temp;
vector<pair<string,double>> revenue;

// Inside the loop
temp.first = "string";
temp.second = map[i].second;
revenue[i].push_back(temp);