Questions tagged [stdmap]

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.

1446 questions
72
votes
6 answers

Find mapped value of map

Is there a way in C++ to search for the mapped value (instead of the key) of a map, and then return the key? Usually, I do someMap.find(someKey)->second to get the value, but here I want to do the opposite and obtain the key (the values and keys are…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
69
votes
3 answers

Checking for existence in std::map - count vs find

So there seem to be two generally acceptable methods of determining whether or not a key exists in a std::map: map.find(key) != map.end() map.count(key) > 0 Is one more efficient than the other? Specifically, the concept of count() could be…
dolphy
  • 6,218
  • 4
  • 24
  • 32
67
votes
3 answers

How can I delete elements of a std::map with an iterator?

I would like to loop through an std::map and delete items based on their contents. How best would this be done?
user542687
63
votes
11 answers

Use of for_each on map elements

I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container? The closest answer I could find was this: Boost.Bind to…
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
62
votes
8 answers

When I should use std::map::at to retrieve map element

I have read different articles on web and questions at stackoverflow, but for me it is not clear is there any exclusive case when it is better to use std::map::at to retrieve map element. According to definition, std::map::at Returns a reference…
T M
  • 3,195
  • 2
  • 31
  • 52
62
votes
8 answers

How to iterate over a std::map full of strings in C++

I have the following issue related to iterating over an associative array of strings defined using std::map. -- snip -- class something { //... private: std::map table; //... } In the constructor I populate table…
crazybyte
  • 9,999
  • 5
  • 26
  • 22
59
votes
6 answers

Why Can't I store references in a `std::map` in C++?

I understand that references are not pointers, but an alias to an object. However, I still don't understand what exactly this means to me as a programmer, i.e. what are references under the hood? I think the best way to understand this would be to…
ng5000
  • 12,330
  • 10
  • 51
  • 64
59
votes
10 answers

What is the best way to use two keys with a std::map?

I have a std::map that I'm using to store values for x and y coordinates. My data is very sparse, so I don't want to use arrays or vectors, which would result in a massive waste of memory. My data ranges from -250000 to 250000, but I'll only have a…
Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
53
votes
7 answers

What is the difference between std::list and std::map in C++ STL?

What is the difference between std::list and std::map? Is there a find method for the list, too?
Boolean
  • 14,266
  • 30
  • 88
  • 129
51
votes
8 answers

C++ std::map holding ANY type of value

Basically I want MyClass that holds a Hashmap that maps Field name(string) to ANY type of Value.. For this purpose I wrote a separate MyField class that holds the type & value information.. This is what I have so far: template class…
user3794186
  • 639
  • 2
  • 8
  • 10
50
votes
6 answers

C++ map vs map performance (I know, "again?")

I was using a map with a std::string key and while everything was working fine I wasn't getting the performance I expected. I searched for places to optimize and improved things only a little and that's when a colleague said, "that string key is…
uroc
  • 3,993
  • 3
  • 21
  • 18
46
votes
7 answers

How can i estimate memory usage of std::map?

For example, I have a std::map with known sizeof(A) and sizeof(B), while map has N entries inside. How would you estimate its memory usage? I'd say it's something like (sizeof(A) + sizeof(B)) * N * factor But what is the factor? Different formula…
Drakosha
  • 11,925
  • 4
  • 39
  • 52
45
votes
2 answers

std::map thread-safety

Is reference to object in std::map is thread safe? std::map< std::string, Object > _objects; map can be changed from many threads and this access is synchronized, but reference to value (Object &) accessable just from 1 instance and thread. is…
dr11
  • 5,166
  • 11
  • 35
  • 77
44
votes
6 answers

How can I traverse/iterate an STL map?

I want to traverse an STL map. I don't want to use its key. I don't care about the ordering, I just look for a way to access all elements it contains. How can I do this?
atoMerz
  • 7,534
  • 16
  • 61
  • 101
44
votes
5 answers

advantages of std::set vs vectors or maps

This may be a stupid question, I am quite new to C++ and programming in general. I wish to understand the use of several STL containers and with that in mind, I was wondering what the advantages are of using std::set vs for example using vectors or…
brunodd
  • 634
  • 1
  • 5
  • 10
1
2
3
96 97