Adding to a vector of pair

Richard picture Richard · Oct 26, 2011 · Viewed 212k times · Source

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:

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:

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

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

Answer

avakar picture avakar · Oct 26, 2011

Use std::make_pair:

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