13

I have the following map structure: map < pair < int,int >, object* > and I wish to insert into it.

How would I do it since I am trying to insert a pair and an object and I must make a pair out of this?

Should I create a new pair using make_pair() out of the pair and object that I have? If so, could you please let me know how to do this?

1
  • 6
    What code have you tried?
    – mmmmmm
    Feb 22, 2010 at 15:26

4 Answers 4

22
object * myObject = // get an object somehow
myMap.insert(std::make_pair(std::make_pair(1,2), myObject));

or

typedef map<pair<int, int>, object *> MapType;
object * myObject = // get an object somehow
myMap.insert(MapType::value_type(std::make_pair(1,2), myObject));
2
  • 1
    I used your first suggestion - nice and concise. Thanks so much!
    – Myx
    Feb 22, 2010 at 15:49
  • 3
    If you're using this a lot in your code you might want to wrap the map up in a class with an insert (pair, object) function for readability.
    – Tom Smith
    Feb 22, 2010 at 16:00
14

Assuming you're using C++11 or later, the best approach is probably:

object * myObject = // get an object somehow
myMap.emplace({1,2}, myObject);

For maps, emplace can be thought of as a version of insert that takes the key and value as separate arguments (it can actually take any combination of arguments that the corresponding pair type's constructors can take). In addition to being syntactically cleaner, it's also potentially more efficient than make_pair, because make_pair will usually produce an output whose type doesn't precisely match the value_type of the container, and so it incurs an unnecessary type conversion.

I used to recommend this, which also only works in C++11 or later:

object * myObject = // get an object somehow
myMap.insert({{1,2}, myObject});

This avoids the slightly surprising use of emplace, but it formerly didn't work if the key or value type are move-only (e.g. unique_ptr). That's been fixed in the standard, but your standard library implementation may not have picked up the fix yet. This might also theoretically be slightly less efficient, but in a way that any halfway decent compiler can easily optimize away.

1
  • I think that the offi cial answer and this one should be merged based on compiler version criteria
    – Aviv
    Dec 3, 2017 at 6:37
1

There are two ways:

typedef std::map<int,Object> map_t;
map_t map;
Object obj;

std::pair<map_t::iterator, bool> result = map.insert(std::make_pair(1,obj)); // 1

map[1] = obj; // 2
  1. Only works if the key is not already present, the iterator points to the pair with the key value and the bool indicates if it has been inserted or not.

  2. Easier, but if it does not already exist the object is first default constructed and then assigned instead of being copy constructed

If you don't have to worry about performance, just choose by whether or not you wish to erase the previous entry.

2
  • Correct, but the question asked about a map with a key type which is also a pair.
    – Nick Meyer
    Feb 22, 2010 at 15:30
  • I fail to see the dependence. sed s/1/std::make_pair(1,1)/g and a suitably defined map_t. It does not change the comments or anything, I just prefer to demonstrate with simple concepts to help focus on the important points rather than hiding them in the crowd. Feb 23, 2010 at 16:05
-1
int a=10,b=20;

map < pair < int,int >, int > m;

pair < int,int >numbers = make_pair(a,b);

int sum=a+b;

m[numbers]=sum;

Our map will have its key as pairs of numbers.We can access the integer values of pair variable using dot(.) operator.

1
  • Can you explain why your answer is preferred over the accepted answer? Code only answers don't provide much information to readers in the future.
    – Simply Ged
    May 27, 2019 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.