c++ - 获取 C++ 映射中元素的索引

标签 c++ map indexing std

我的 C++ 应用程序中有一个名为 myMapstd::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/

相关文章:

c++ - 您的计算机中缺少 api-ms-win-core-winrt-string-l1-1-0.dll

c++ - WinAPI C ListView

map - 如何计算 map 上的线段交点

c++ - boost::flyweight 作为 std::map 中的值

java - 更新对象中的 ArrayList 索引

matlab - 使用向量索引没有线性索引的矩阵

c++ - 埃拉托色尼筛法 C++

C++矩阵类模板

c++ - 初始化值为 std::vector 的 std::map 的最佳方法是什么?

python - 列表索引效率(python 2 vs python 3)