std::map is a class in the C++ Standard Library. It is a sorted associative container that contains key-value pairs with unique keys. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.
Questions tagged [stdmap]
1446 questions
531
votes
12 answers
Initializing a static std::map in C++
What is the right way of initializing a static map? Do we need a static function that will initialize it?

Nithin
- 5,331
- 3
- 17
- 8
343
votes
24 answers
How to retrieve all keys (or values) from a std::map and put them into a vector?
This is one of the possible ways I come out:
struct RetrieveKey
{
template
typename T::first_type operator()(T keyValuePair) const
{
return keyValuePair.first;
}
};
map m;
vector keys;
//…

Owen
- 3,851
- 2
- 18
- 12
211
votes
13 answers
In STL maps, is it better to use map::insert than []?
A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std::make_pair(key, value)).
I just asked him and…

danio
- 8,548
- 6
- 47
- 55
171
votes
9 answers
What is the preferred/idiomatic way to insert into a map?
I have identified four different ways of inserting elements into a std::map:
std::map function;
function[0] = 42;
function.insert(std::map::value_type(0, 42));
function.insert(std::pair(0,…

fredoverflow
- 256,549
- 94
- 388
- 662
124
votes
16 answers
std::map default value
Is there a way to specify the default value std::map's operator[] returns when an key does not exist?

anon
- 41,035
- 53
- 197
- 293
115
votes
4 answers
Recommended way to insert elements into map
Possible Duplicate:
In STL maps, is it better to use map::insert than []?
I was wondering, when I insert element into map, what is the recommended way. Should I
map[key] = value;
or
map.insert(std::pair(key, value));
I did…

Cheok Yan Cheng
- 47,586
- 132
- 466
- 875
112
votes
4 answers
How can I create my own comparator for a map?
typedef map myMap;
When inserting a new pair to myMap, it will use the key string to compare by its own string comparator. Is it possible to override that comparator? For example, I'd like to compare the key string by its length,…

Xitrum
- 7,765
- 26
- 90
- 126
107
votes
8 answers
std::map insert or std::map find?
Assuming a map where you want to preserve existing entries. 20% of the time, the entry you are inserting is new data. Is there an advantage to doing std::map::find then std::map::insert using that returned iterator? Or is it quicker to attempt the…

Superpolock
- 3,515
- 7
- 30
- 24
107
votes
6 answers
How to update std::map after using the find method?
How to update the value of a key in std::map after using the find method?
I have a map and iterator declaration like this:
map m1;
map ::iterator m1_it;
typedef pair count_pair;
I'm using the map to store the…

jaykumarark
- 2,359
- 6
- 35
- 53
91
votes
5 answers
How can I merge two STL maps?
How can I merge two STL maps into one? They both have the same key and value types (map). If there is an overlap of the keys, I would like to give preference to one of the maps.

JonF
- 2,336
- 6
- 38
- 57
90
votes
10 answers
Using char* as a key in std::map
I am trying to figure out why the following code is not working, and I am assuming it is an issue with using char* as the key type, however I am not sure how I can resolve it or why it is occuring. All of the other functions I use (in the HL2 SDK)…

Josh Renwald
- 1,479
- 3
- 17
- 17
90
votes
8 answers
How can I use std::maps with user-defined types as key?
I'm wondering why I can't use STL maps with user-defined classes. When I compile the code below, I get the following cryptic error message. What does it mean? Also, why is it only happening with user-defined types? (Primitive types are okay when…

unknown
- 1,313
- 3
- 12
- 7
87
votes
6 answers
How can I get a value from a map?
I have a map named valueMap as follows:
typedef std::mapMAP;
MAP valueMap;
...
// Entering data.
Then I am passing this map to a function by reference:
void function(const MAP &map)
{
std::string value = map["string"];
…

Aneesh Narayanan
- 3,220
- 11
- 31
- 48
85
votes
7 answers
What is difference between const and non const key?
What is the difference between the following two lines?
map map_data;
map map_data;

NullPoiиteя
- 56,591
- 22
- 125
- 143
73
votes
5 answers
Last key in a std::map
I am looking for the highest key value (a defined by the comparison operator) of a std::map.
Is this guaranteed to be
map.rbegin()->first
?
(I am a bit shaky on reverse iterators, and how much freedom there is in the implementation of std::map)…

peterchen
- 40,917
- 20
- 104
- 186