c++ - 尝试设置迭代器时出现奇怪的错误

标签 c++

这可能是一个愚蠢的错误,但我似乎无法找到我做错了什么。

我得到的错误是没有运算符“=”匹配这些操作数

这是我的代码...

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/

相关文章:

c++ - 使用仿函数改变大小写

c++ - 从二进制文件中读取对象

c++ - Qt 发送 keyPressEvent

c++ - 如何检索 Win32 Windows Mobile 平台的进程 ID?

c++ - 转换为整数类型或其他类型不能出现在常量表达式中

c++ - MPI_Bcast与数组:每个进程发送一个元素

c++ - 如何分析和/或消除由于内存分配引起的性能变化?

c++ - 通过指针调用函数而不从对象分配内存

c++ - 如何正确使用 noexcept 运算符

c++ - 从选择的语句中删除选择