C++ 添加到对的向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7897050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding to a vector of pair
提问by Richard
I have a vector
of pair
like such:
我有一个vector
的pair
像这样的:
vector<pair<string,double>> revenue;
I want to add a string and a double from a map like this:
我想从这样的地图中添加一个字符串和一个双精度值:
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
?
但这说不能有两个论点。那么,如何可以添加到这个vector
的pair
?
回答by avakar
回答by m47h
IMHO, a very nice solution is to use c++11 emplace_backfunction:
恕我直言,一个非常好的解决方案是使用 c++11 emplace_back函数:
revenue.emplace_back("string", map[i].second);
It just creates a new element in place.
它只是在适当的位置创建了一个新元素。
回答by Ed S.
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,它保存一个Pair
. 所以...
revenue.push_back( std::make_pair( "string", map[i].second ) );
回答by Hsu Hau
Or you can use initialize list:
或者您可以使用初始化列表:
revenue.push_back({"string", map[i].second});
回答by hochl
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++ 的好资源,尽管后者似乎是最近的首选参考。
回答by Caner SAYGIN
revenue.push_back(pair<string,double> ("String",map[i].second));
this will work.
这会起作用。
回答by Sardeep Lakhera
You can use std::make_pair
您可以使用 std::make_pair
revenue.push_back(std::make_pair("string",map[i].second));
回答by Ritankar Paul
Using emplace_back
function is way better than any other method since it creates an object in-place of type T
where vector<T>
, whereas push_back
expects an actual value from you.
使用emplace_back
function 比任何其他方法都要好得多,因为它在T
where类型的地方创建了一个对象vector<T>
,而push_back
期望来自您的实际值。
vector<pair<string,double>> revenue;
// make_pair function constructs a pair objects which is expected by push_back
revenue.push_back(make_pair("cash", 12.32));
// emplace_back passes the arguments to the constructor
// function and gets the constructed object to the referenced space
revenue.emplace_back("cash", 12.32);
回答by sarthak bansal
As many people suggested, you could use std::make_pair
.
正如许多人建议的那样,您可以使用std::make_pair
.
But I would like to point out another method of doing the same:
但我想指出另一种方法:
revenue.push_back({"string",map[i].second});
push_back() accepts a single parameter, so you could use "{}" to achieve this!
push_back() 接受单个参数,因此您可以使用“{}”来实现这一点!
回答by Dheeraj Kumar
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.push_back(temp);