gpt4 book ai didi

c++ - ordered_map() 中 ‘.’ 标记之前的预期主表达式

转载 作者:行者123 更新时间:2023-11-30 04:15:20 29 4
gpt4 key购买 nike

我想插入段落或文章内容并处理每个单词。下面我试图获取每个字符串,然后获取它的出现。最后我想要出现次数最多的词。我是 C++ 的新手。现在我已经静态插入了两个字符串。这会给出错误 expected primary-expression before ‘.’ token。代码如下:`

#include <string>
#include <iostream>
#include <unordered_map>

int main()
{
typedef std::unordered_map<std::string,int> occurrences;
occurrences s1;
s1.insert(std::pair<std::string,int>("Hello",1));
s1.insert(std::pair<std::string,int>("Hellos",2));

//for ( auto it = occurrences.begin(); it != occurrences.end(); ++it ) this also gives same + additional " error: unable to deduce ‘auto’ from ‘<expression error>’" error
for (std::unordered_map<std::string, int>::iterator it = occurrences.begin();//Error is here
it != occurrences.end(); ////Error is here
++it)
{
std::cout << "words :" << it->first << "occured" << it->second << "times";
}

return 0;
}

错在哪里?

最佳答案

occurrences 是一种类型,而不是对象。您想要使用对象 s1

版本 1:

for (auto it = s1.begin(); it != s1.end(); ++it)

版本 2:

for (std::unordered_map<std::string, int>::iterator it = s1.begin(); it != s1.end(); ++it)

版本 3:

for (auto pair : s1)

关于c++ - ordered_map() 中 ‘.’ 标记之前的预期主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18395183/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com