应在“.”之前使用主表达式ordered_map()中的令牌
expected primary-expression before ‘.’ token in ordered_map()
我想插入段落或文章内容并处理每个单词。下面我试图得到每个字符串,然后得到它的出现次数。最后我想要最大出现次数的单词。我是c++新手。现在我已经静态地插入了两个字符串。这给出了错误CCD_ 1。代码如下:`
#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)
您需要使用s1.begin()
和s1.end()
,而不是occurrences.begin()
和occurrences.end()
,因为occurrences
是一种类型,而s1
是该类型的变量。
相关文章:
- 如何在C++中使用X509证书模在令牌中查找私钥
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 错误:令牌 { '{' 之前应存在非限定 ID
- 允许哪些令牌作为 #include 的参数?
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 它在 { 令牌之前给了我预期的标识符
- (错误:令牌之前'<<'预期的主表达式)
- 为什么 ## aka 令牌粘贴运算符不适用于 C 和 C++ 中的注释?
- 错误:"->"令牌之前的预期初始值设定项
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 丢失读入的每一行输入中的最后一个令牌
- 如何使用 libCurl 将访问令牌发送到服务器 API
- C++令牌定义成员
- 宏定义中的预处理器令牌两边有两个双引号
- 错误:C++中']'令牌之前的预期主表达式
- 错误:令牌之前的预期主表达式'&&'
- 令牌之前']'预期的主表达式?
- 根据特定条件从文件中检索每个令牌