Richard
Richard

Reputation: 6126

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?

Upvotes: 67

Views: 290638

Answers (10)

sarthak bansal
sarthak bansal

Reputation: 43

As many people suggested, you could use 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!

Upvotes: 0

Ritankar Paul
Ritankar Paul

Reputation: 75

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.

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);

Upvotes: 1

Dheeraj Kumar
Dheeraj Kumar

Reputation: 21

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);

Upvotes: 0

Sardeep Lakhera
Sardeep Lakhera

Reputation: 319

You can use std::make_pair

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

Upvotes: 0

Caner SAYGIN
Caner SAYGIN

Reputation: 537

revenue.push_back(pair<string,double> ("String",map[i].second));

this will work.

Upvotes: 4

Hsu Hau
Hsu Hau

Reputation: 622

Or you can use initialize list:

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

Upvotes: 11

m47h
m47h

Reputation: 1703

IMHO, a very nice solution is to use c++11 emplace_back function:

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

It just creates a new element in place.

Upvotes: 47

hochl
hochl

Reputation: 12950

Read the following documentation:

http://cplusplus.com/reference/std/utility/make_pair/

or

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.

Upvotes: 6

avakar
avakar

Reputation: 32635

Use std::make_pair:

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

Upvotes: 123

Ed Swangren
Ed Swangren

Reputation: 124732

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...

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

Upvotes: 11

Related Questions