3

I have this map<string, vector <pair<int, int> > > variable and I'm pushing back a value, but code::blocks is telling me that pair does not have a member function called push_back. What should I do to get it to push back pairs rather than pair<>.push_back()?

This is basically what im doing:

map<string, vector <pair<int, int> > > T;
for(int x = 0; x < data.size(); x++)
     T[data[x].str].push_back(data[x].PAIR)

and the error is:

error: no matching function for call to 'std::vector<std::pair<int, int>,
  std::allocator<std::pair<int, int> > >::push_back(std::map<int, int, 
    std::less<int>, std::allocator<std::pair<const int, int> > >&)'
Soo Wei Tan
  • 3,262
  • 2
  • 34
  • 36
calccrypto
  • 8,583
  • 21
  • 68
  • 99

3 Answers3

6

Not sure about you problem.

Following code works fine for me:

map<string, vector <pair<int, int> > > T;
pair<int, int> p;
p.first = 1;
p.second = 10;
T["Hello"].push_back(p);
cout << T["Hello"][0].first << endl;
Elalfer
  • 5,312
  • 20
  • 25
3

The message indicates that you are trying to push back a std::map, not a pair. What does your data structure look like?

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • its just a bunch of public variables (i know its not good practice). the two variables here are `string day` and `pair T` – calccrypto Apr 13 '11 at 20:34
2

Vectors do have push_back() method. Most likely data[x].PAIR is not of type pair. What type is data[x].PAIR? If you convert data[x].PAIR to pair it should work.

Antti Huima
  • 25,136
  • 3
  • 52
  • 71