简体   繁体   中英

C++ How to insert in std::multimap?

How can i insert in std::multimap <bool life, int id, std::pair < x, y >> ?

I ll use it for storing data's player and ia, Is it the best container for this?

Not good at all.

The signature of a multimap is as follows:

template < class Key,                                   // multimap::key_type
           class T,                                     // multimap::mapped_type
           class Compare = less<Key>,                   // multimap::key_compare
           class Alloc = allocator<pair<const Key,T> >  // multimap::allocator_type
           > class multimap;

The use of your multimap is wrong.

The key is bool which means you use only two nodes 0 and 1 (or false´ and true`). So each inserted element is in either one of them, and thus you are actually enlisting elements with the same key. This is inefficient.

The value is an int . Well, alright though I wonder why would you want to map a bool to an int .

And lastly the error in your signature: pair is not a compare function , but the third template argument must be a compare function . If you leave it empty then by default if would be less<bool> (because you chose bool as the key ).

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