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?
To insert/append a vector's elements to another vector, we use vector::insert() function.
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.
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.
Use std::make_pair:
revenue.push_back(std::make_pair("string",map[i].second)); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With