尝试设置迭代器时会遇到奇怪的错误



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

我遇到的错误是no operator "=" matches these operands

这是我的代码...

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循环中获得错误,如果我不能迭代它,我将无法继续打印出地图。

使用const迭代器:

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++) {
    }
}

使用const_iteratorauto

void print_words(const map < string, int >& m1) {
    cout << "Number of non-empty words: " << m1.size() << 'n';
    int count = 0;
    for (auto it = m1.cbegin(); it != m1.cend(); it++) {
    }
}

最新更新