58

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?

bltxd
  • 8,527
  • 5
  • 29
  • 44
Richard
  • 5,460
  • 29
  • 114
  • 197
  • So you don't have a vector pair but a pair vector, or vector of pairs. Once you understand this, you have solved your problem. – Christian Rau Oct 26 '11 at 01:06

10 Answers10

115

Use std::make_pair:

revenue.push_back(std::make_pair("string",map[i].second));
avakar
  • 31,285
  • 9
  • 64
  • 102
  • 2
    What about emplace_back? – Mr.WorshipMe May 13 '15 at 12:12
  • 1
    @Mr.WorshipMe, you can use it in C++11. See an answer below for an example. I'm not sure what you're asking though. – avakar May 13 '15 at 12:59
  • 2
    I was pointing to a more elegant and possibly faster solution... I didn't see the under-rated answer below... You might want to update your answer... – Mr.WorshipMe May 13 '15 at 14:05
  • Would this approach differ in a constructor ? (eg. I have a class containing a vector of pairs and want to read from a two dimensional initializer_list. Where the inner one is supposed to be the pair. So I loop and `vec.push_back(make_pair ...)` like this) – gr4nt3d Jan 09 '16 at 17:43
41

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.

m47h
  • 1,541
  • 2
  • 17
  • 24
11
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 ) );     
Raghav Sood
  • 80,914
  • 21
  • 183
  • 190
Ed S.
  • 119,398
  • 20
  • 176
  • 254
10

Or you can use initialize list:

revenue.push_back({"string", map[i].second});
Hsu Hau
  • 544
  • 5
  • 15
6

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.

hochl
  • 11,784
  • 10
  • 53
  • 83
4
revenue.push_back(pair<string,double> ("String",map[i].second));

this will work.

Caner SAYGIN
  • 499
  • 3
  • 10
0

You can use std::make_pair

revenue.push_back(std::make_pair("string",map[i].second));
Sardeep Lakhera
  • 369
  • 3
  • 14
0

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);
Ritankar Paul
  • 65
  • 1
  • 4
0

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!

-1

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