所以我必须为一个刽子手游戏编写这个代码,并且我提供了一个dicitonary.txt文件,它在一个单独的行上包含127k个单词。我做的是我使用map<int, set<string>> DICTIONARY;
根据字母数来存储单词。所以我在这里有这个代码,但我没有什么问题:
#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main(){
map<int, set<string>> DICTIONARY;
try{
ifstream file("dictionary.txt");
if(file.is_open()){
string TEMP_INPUT;
for(int i = 0; i < 127143; i++){
file >> TEMP_INPUT;
DICTIONARY[TEMP_INPUT.length()].insert(TEMP_INPUT);
}
}
else throw 404;
}catch(int x){
cout << "Error: " << x << " - file not found!" << endl;
}
return 0;
}
但我得到的是一个错误,说DICTIONARY没有在那个范围内定义。我知道if(){}
等中的内容留在其中,它被称为范围。但是我应该如何访问该地图?
我假设一个指针,但仍然不知道如何在范围内使用它。
如果有人也可以告诉我如何使用if
语句中的127 ..而不是i
而不是while
我可以使用source.cpp In function 'int main()':
source.cpp:14:24: error: 'DICTIONARY' was not declared in this scope
source.cpp:14:21: error: '>>' should be '> >' within a nested template argument list
非文件结尾或类似的东西?
错误输出为:
{{1}}
非常感谢你!
答案 0 :(得分:1)
map<int, set<string>>
在语法上符合C ++直到C ++ 11。错误'DICTIONARY' was not declared in this scope
具有误导性,原因是编译器不接受map<int, set<string>> DICTIONARY
作为变量声明,并且没有将DICTIONARY添加到标识符表中。
将其替换为map<int, set<string> >
(注意添加的空格)
...或者如果编译器支持,则尝试将代码编译为C ++ 11。 C ++ 11为C ++带来了重大改进!