[英]Std iterator assignment error
如果這個問題很愚蠢,請忍受我。
頭文件中定義了以下內容:
typedef char NAME_T[40];
struct NAME_MAPPING_T
{
NAME_T englishName;
NAME_T frenchName;
};
typedef std::vector<NAME_MAPPING_T> NAMES_DATABASE_T;
后來需要找到一個特定的英文名稱:
const NAMES_DATABASE_T *wordsDb;
string str;
std::find_if( wordsDb->begin(),
wordsDb->end(),
[str](const NAME_MAPPING_T &m) -> bool { return strncmp(m.englishName, str.c_str(), sizeof(m.englishName)) == 0; } );
這段代碼(老實說,我復制粘貼了)可以編譯,但是如果我想檢查find_if()返回的值,像這樣:
NAMES_DATABASE_T::iterator it;
it = std::find_if(blah ..)
該代碼將無法編譯!
實際上, 它的行= std :: find_if(...)將返回錯誤:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<_Myvec>' (or there is no acceptable conversion)
怎么了 ?
謝謝你的時間。
const NAMES_DATABASE_T *wordsDb;
您的wordsDb
是const,因此wordsDb->begin()
返回const迭代器,因此find_if
返回const迭代器。 您正在嘗試將該常量迭代器分配給非常量NAMES_DATABASE_T::iterator it
,因此出現錯誤。
您可以使用NAMES_DATABASE_T::const_iterator
獲取const迭代器。 並且,除非有一些其他特殊情況,否則應使用std::string
代替那些char緩沖區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.