c++ - 在迭代器问题 C++ 中访问结构

标签 c++ c++11 mingw

我试图在 C++ 中访问迭代器中的结构元素,但编译器只给我一个错误,指出该结构不包含该元素。我正在尝试执行以下操作:

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++;
    }
}

我收到以下消息:

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'

为什么这不可能?

最佳答案

您可能应该这样重写 for 循环的主体:

if (itr->str.compare(currentWord)==0)
//     ^^
{
    isPresent = true;
    itr->frequenzy++;
//     ^^
}

. 运算符的优先级高于 * 运算符。因此,如果你真的想使用这两个运算符,你应该这样重写上面的内容:

if ((*itr).str.compare(currentWord)==0)
//  ^^^^^^^
{
    isPresent = true;
    (*itr).frequenzy++;
//  ^^^^^^^
} 

关于c++ - 在迭代器问题 C++ 中访问结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464190/

相关文章:

c++ - native C++ 将 IIterable 传递给 WinRT

c++ - 使用数据库模型(键)来引用运行时对象,好主意还是坏主意?

c++ - 扫描字符串每个字符的ASCII值

在 minGW-W64 g++ 中编译的 C++ 代码不能用 Ubuntu g++ 编译

c++ - 在 C++ 中读取字节

gcc - 如何在 Win 7 上设置 gtk

c - 为什么我可以用 GCC 制作的最小编译 exe 是 67KB?

c++ - Clang AST 匹配器 : How to find calls to a perfectly forwarding function called with rvalues?

c++ - 强制转换为 C++ 中的字符串运算符重载

c - GCC-将int分配给char时不应该发出警告吗?