尝试设置迭代器时出现奇怪的错误 [英] Getting strange error while trying to set up an iterator

查看:87
本文介绍了尝试设置迭代器时出现奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我得到的错误是没有操作员=匹配这些操作符号。



这里是我的代码...



This is probably a silly error but i cant seem to find what i have done wrong.

The error i am getting is 'no operator "=" matches these opperands'.

Here is my code...

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循环中得到错误,如果我不能继续打印出地图迭代它,所以任何帮助将不胜感激。



我尝试过:



我试图找到任何简单的错别字或错误但找不到任何错误。我在另一个方法中设置了一个迭代器,它工作得很好,所以我真的很难理解为什么我会收到这个错误。


I get the error in the for loop in the it = m1.begin() statement and i cannot go on to print out the map if i cant iterate through it so any help would be much appreciated.

What I have tried:

I tried finding any simple typos or errors but cannot find any. I set up an iterator in another method and it worked completely fine so i am really stumped on why i am getting this error.

推荐答案

只需使用 auto 关键字来定义变量类型。 C ++编译器会为你推断出正确的类型。



Just use auto keyword to define your variable type. C++ compiler will infer the correct type for you.

void print_words(const std::map < std::string, int >& m1) {
	std::cout << "Number of non-empty words: " << m1.size() << '\n';

	int count = 0;
	for (auto it = m1.begin(); it != m1.end(); ++it) {

	}
}


这篇关于尝试设置迭代器时出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆