简体   繁体   中英

map<string, vector <pair<int, int> > > pushing back into pair?

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> > >&)'

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;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM