Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding to a vector of pair

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?

like image 684
Richard Avatar asked Oct 25 '11 23:10

Richard


People also ask

Can you add a vector to another vector?

To insert/append a vector's elements to another vector, we use vector::insert() function.

Can a pair contain a vector?

A pair is a container which stores two values mapped to each other, and a vector containing multiple number of such pairs is called a vector of pairs.

How do you add an element to a vector?

To add elements to vector, you can use push_back() function. push_back() function adds the element at the end of this vector. Thus, it increases the size of vector by one.


1 Answers

Use std::make_pair:

revenue.push_back(std::make_pair("string",map[i].second)); 
like image 85
avakar Avatar answered Oct 10 '22 23:10

avakar