C++迭代映射

Din*_*esh 2 c++ stl map

众所周知,以下代码用于在C++中迭代map

for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
    std::cout << itr->first << " => " << itr->second << '\n';
}
Run Code Online (Sandbox Code Playgroud)

其中itr声明为std :: map :: iterator.第一个和第二个成员既不在std :: map中也不在std :: iterator中声明.那怎么可以访问?

jua*_*nza 8

a的元素std::mapstd::pair<key_type, mapped_type>,因此取消引用map迭代器会为您提供对其中一个的引用.

它是std::pair具有firstsecond成员的类模板.