添加到对的向量 [英] Adding to a vector of pair

查看:112
本文介绍了添加到对的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量的对像这样:

vector<pair<string,double>> revenue;

我要从地图中添加一个字符串和一个double,如下所示:

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

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

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

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

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

但是说不能接受两个参数。那么如何添加向量的

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

推荐答案

使用 std :: make_pair

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

这篇关于添加到对的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆