簡體   English   中英

標准迭代器分配錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM