[英]Iterator error C2440
I am trying to make a small hashing program but there is an error I don't know how to deal with. 我正在尝试制作一个小的哈希程序,但是有一个错误,我不知道该如何处理。 The problem is on the arrow and here is the error: 问题在箭头上,这是错误:
Error 1 error C2440: 'initializing' : cannot convert from 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' to 'std::_String_iterator<_Elem,_Traits,_Alloc>' 错误1错误C2440:'正在初始化':无法从'std :: _ String_const_iterator <_Elem,_Traits,_Alloc>'转换为'std :: _ String_iterator <_Elem,_Traits,_Alloc>'
My code: 我的代码:
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
unsigned long hash(const string& str);
int main()
{
long out;
string word;
word = "about";
out = hash(word) % 255;
cout << out;
system("pause");
return 0;
}
unsigned long djb2(const string& str)
{
unsigned long hash = 5381;
for(string::iterator it=str.begin();it!=str.end();it++) //<~~~~~~~~~~
hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
return hash;
}
You need to use a const_iterator
: 您需要使用const_iterator
:
for( std::string::const_iterator it=str.begin();it!=str.end();it++)
since your argument to the function is const string &str
the iterator
has to agree. 由于您对该函数的参数是const string &str
因此iterator
必须同意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.