关于map和iterator的C++错误

L.W*_*ard -1 c++ iterator

使用代码:

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::map<int, std::map<std::string, int>> mm;

    std::map<std::string, int>::iterator iter = mm.find(1);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Class 'std::_Rb_tree_iterator<std::pair<const int,
std::map<std::string, int>>>' is not compatible with class
'std::map<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char>>, int>::iterator'
Run Code Online (Sandbox Code Playgroud)

我没有编译代码.Clion提醒我,上面有一个错误.

Edg*_*jān 9

你只需使用错误的类型iter,它应该是:

std::map<int, std::map<std::string, int>>::iterator
Run Code Online (Sandbox Code Playgroud)

但是,我建议使用auto以避免此类错误:

auto iter = mm.find(1);
Run Code Online (Sandbox Code Playgroud)