这可能是一个愚蠢的错误,但我似乎无法找到我做错了什么。
我得到的错误是没有运算符“=”匹配这些操作数
。
这是我的代码...
void print_words(const map < string, int >& m1) {
map<string, int>::iterator it;
cout << "Number of non-empty words: " << m1.size() << '\n';
int count = 0;
for (it = m1.begin(); it != m1.end(); it++) {
}
}
我在 it = m1.begin()
语句的 for 循环中遇到错误,如果我不能遍历它,我就无法继续打印出 map 。
最佳答案
使用常量迭代器:
void print_words(const map < string, int >& m1) {
cout << "Number of non-empty words: " << m1.size() << '\n';
int count = 0;
for (map<string, int>::const_iterator it = m1.cbegin(); it != m1.cend(); it++) {
}
}
关于c++ - 尝试设置迭代器时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509117/