I use the find() method std::map , which returns an iterator.
However, I need the index of the item found; for example: 0, which corresponds to std::map::begin() , etc.
#include <map> #include <utility> #include <iostream> int main() { std::map< int, int > aMap; aMap.insert( std::make_pair(100, 50) ); aMap.insert( std::make_pair(200, 40) ); aMap.insert( std::make_pair(300, 60) ); std::map< int, int >::iterator it_map = aMap.find(300); if (it_map != aMap.end()) std::cout << it_map << "\n"; // error }
This does not compile, and I know the reason. However, I need a print method of 2 because the index 300 is 2.
For this simple example, you can say that a map (binary tree) is not a good container. However, in real code there are tons of elements that I need to look for, and a binary tree is suitable for this.
Any idea?
source share