Extracting STL Card Keys

Is there a way (besides saving the key as part of the value and iterating over the map) to extract the keys from the STL map, multimap (hash_map) a la Perl keys (% hash)?

+3
source share
3 answers
for (std::map<key, value>::iterator iter = m.begin(); iter != m.end(); ++iter)
    iter->first; // this is the key
+14
source

If you often need to get these keys (for example, in a large loop), you might be interested in using boost :: bimap . Otherwise, you can use the Nikola solution correctly.

Sometimes I add keys to another container when adding items to the map. To do this, it is necessary to synchronize the two containers, but if it is sufficiently isolated (in the class), then it is easy to configure.

0
source

You can use a for loop.

for (const auto & keyVal : myMap)
    keyVal.first;
0
source

Source: https://habr.com/ru/post/1774602/


All Articles