Get index of element in C++ map
c++, dictionary, indexing, std
Solution
A `std::map` doesn't really have an index, instead it has an iterator for a key / value pair. This is similar to an index in that it represents a position of sorts in the collection but it is not numeric. To get the iterator of a key / value pair use the `find` method
std::map<string, int>::iterator it = myMap.find("myKey");
Problem
I have a `std::map` called `myMap` in my C++ application, and I want to get an element using either `myMap.find(key)` or `myMap[key]`. However, I would also like to get the index of that element in the map. ``` std::map<string, int> myMap; // Populate myMap with a bunch of items... myElement = myMap["myKey"]; // Now I need to get the index of myElement in myMap ``` Is there a clean way to do that? Thank you.