访问迭代器问题C+中的结构+; 我试图访问C++中迭代器中的Stutt元素,但是编译器只给了我一个错误,即Strut不包含那个元素。我正在努力做到以下几点: typedef struct { string str; int frequenzy; } word; bool isPresent = false; for(std::vector<word>::iterator itr=words.begin(); itr!=words.end(); ++itr) { if(*itr.str.compare(currentWord)==0){ isPresent = true; *itr.frequenzy++; } } typedef结构 { 字符串str; 整数频率; }字; bool isPresent=false; for(std::vector::iterator itr=words.begin();itr!=words.end();++itr) { if(*itr.str.compare(currentWord)==0){ isPresent=true; *itr.frequenzy++; } }
我得到了以下信息:访问迭代器问题C+中的结构+; 我试图访问C++中迭代器中的Stutt元素,但是编译器只给了我一个错误,即Strut不包含那个元素。我正在努力做到以下几点: typedef struct { string str; int frequenzy; } word; bool isPresent = false; for(std::vector<word>::iterator itr=words.begin(); itr!=words.end(); ++itr) { if(*itr.str.compare(currentWord)==0){ isPresent = true; *itr.frequenzy++; } } typedef结构 { 字符串str; 整数频率; }字; bool isPresent=false; for(std::vector::iterator itr=words.begin();itr!=words.end();++itr) { if(*itr.str.compare(currentWord)==0){ isPresent=true; *itr.frequenzy++; } },c++,c++11,mingw,C++,C++11,Mingw,我得到了以下信息: lab7.cc: In function 'int main()': lab7.cc:27:13: error: 'std::vector<word>::iterator' has no member named 'str' lab7.cc:29:11: error: 'std::vector<word>::iterator' has no member named 'frequen zy' lab7.cc:在函数“int main()”中: lab
lab7.cc: In function 'int main()':
lab7.cc:27:13: error: 'std::vector<word>::iterator' has no member named 'str'
lab7.cc:29:11: error: 'std::vector<word>::iterator' has no member named 'frequen
zy'
lab7.cc:在函数“int main()”中:
lab7.cc:27:13:错误:“std::vector::iterator”没有名为“str”的成员
lab7.cc:29:11:错误:“std::vector::iterator”没有名为“frequen”的成员
zy'
为什么不可能呢?您可能应该这样重写
for
循环的主体:
if (itr->str.compare(currentWord)==0)
// ^^
{
isPresent = true;
itr->frequenzy++;
// ^^
}
if ((*itr).str.compare(currentWord)==0)
// ^^^^^^^
{
isPresent = true;
(*itr).frequenzy++;
// ^^^^^^^
}
运算符的优先级高于*
运算符。因此,如果您真的想使用这两个运算符,您应该以这种方式重写上述内容:
if (itr->str.compare(currentWord)==0)
// ^^
{
isPresent = true;
itr->frequenzy++;
// ^^
}
if ((*itr).str.compare(currentWord)==0)
// ^^^^^^^
{
isPresent = true;
(*itr).frequenzy++;
// ^^^^^^^
}
您可能应该这样重写
for
循环的主体:
if (itr->str.compare(currentWord)==0)
// ^^
{
isPresent = true;
itr->frequenzy++;
// ^^
}
if ((*itr).str.compare(currentWord)==0)
// ^^^^^^^
{
isPresent = true;
(*itr).frequenzy++;
// ^^^^^^^
}
运算符的优先级高于*
运算符。因此,如果您真的想使用这两个运算符,您应该以这种方式重写上述内容:
if (itr->str.compare(currentWord)==0)
// ^^
{
isPresent = true;
itr->frequenzy++;
// ^^
}
if ((*itr).str.compare(currentWord)==0)
// ^^^^^^^
{
isPresent = true;
(*itr).frequenzy++;
// ^^^^^^^
}