cap*_*onk 2 c++ iterator visual-studio-2008
我正在尝试制作一个小的哈希程序,但有一个错误,我不知道如何处理.问题出在箭头上,这是错误:
错误1错误C2440:'初始化':无法从'std :: _ String_const_iterator <_Elem,_Traits,_Alloc>'转换为'std :: _ String_iterator <_Elem,_Traits,_Alloc>'
我的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
你需要使用const_iterator:
for( std::string::const_iterator it=str.begin();it!=str.end();it++)
Run Code Online (Sandbox Code Playgroud)
因为你的函数参数是const string &str在iterator有同意.
| 归档时间: |
|
| 查看次数: |
1459 次 |
| 最近记录: |