我的 C++ 应用程序中有一个名为 myMap
的 std::map
,我想使用 myMap.find(key)
或 myMap[key]
。但是,我还想获取该元素在 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
有没有一种干净的方法可以做到这一点?
谢谢。
最佳答案
我来这里是为了寻找这个答案,但我找到了这个 distance 函数需要 2 个迭代器并返回一个索引
cout << distance(mymap.begin(),mymap.find("198765432"));
希望这会有所帮助:D
关于c++ - 获取 C++ 映射中元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863079/