Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+的异常/错误行为+;节目 < >我的代码是为了告诉用户输入的字符串是否是C++中的关键字。 我正在将文件中的关键字读入一个集合,然后检查用户提供的字符串是否在其中 #include <iostream> #include <string> #include <set> #include <algorithm> #include <fstream> using namespace std; int main() { set<string> key; fstream fs; string b; fs.open("keywords.txt",fstream::in); while(getline(fs,b)) key.insert(b); b.clear(); for(auto x:key) cout << x << endl; cout << "Enter String user\nPress exit to terminate\n"; while(getline(cin,b)) { if(b == "exit") break; if(key.find(b) != key.end()) cout << "This is a keyword\n"; else cout << "This is a not a keyword\n"; b.clear(); } fs.close(); } #包括 #包括 #包括 #包括_C++_String_Set - Fatal编程技术网

C+的异常/错误行为+;节目 < >我的代码是为了告诉用户输入的字符串是否是C++中的关键字。 我正在将文件中的关键字读入一个集合,然后检查用户提供的字符串是否在其中 #include <iostream> #include <string> #include <set> #include <algorithm> #include <fstream> using namespace std; int main() { set<string> key; fstream fs; string b; fs.open("keywords.txt",fstream::in); while(getline(fs,b)) key.insert(b); b.clear(); for(auto x:key) cout << x << endl; cout << "Enter String user\nPress exit to terminate\n"; while(getline(cin,b)) { if(b == "exit") break; if(key.find(b) != key.end()) cout << "This is a keyword\n"; else cout << "This is a not a keyword\n"; b.clear(); } fs.close(); } #包括 #包括 #包括 #包括

C+的异常/错误行为+;节目 < >我的代码是为了告诉用户输入的字符串是否是C++中的关键字。 我正在将文件中的关键字读入一个集合,然后检查用户提供的字符串是否在其中 #include <iostream> #include <string> #include <set> #include <algorithm> #include <fstream> using namespace std; int main() { set<string> key; fstream fs; string b; fs.open("keywords.txt",fstream::in); while(getline(fs,b)) key.insert(b); b.clear(); for(auto x:key) cout << x << endl; cout << "Enter String user\nPress exit to terminate\n"; while(getline(cin,b)) { if(b == "exit") break; if(key.find(b) != key.end()) cout << "This is a keyword\n"; else cout << "This is a not a keyword\n"; b.clear(); } fs.close(); } #包括 #包括 #包括 #包括,c++,string,set,C++,String,Set,问题是,我的程序正确读取了所有关键字,但对于其中一些关键字,如false、public,它无法在集中找到它们 i、 e.当我输入false作为用户输入时 它说,“这不是一个关键字。”考虑到您的输入文件,我认为您有一些带有尾随空格的关键字名称 "catch " "false " 在插入集合之前,可以使用boost::trim或您自己的trim修剪字符串以删除空格(请参见) (如果您需要有关代码的建议: 对于输入文件流,可以像这样使用std::ifstream: st

问题是,我的程序正确读取了所有关键字,但对于其中一些关键字,如false、public,它无法在集中找到它们

i、 e.当我输入false作为用户输入时
它说,“这不是一个关键字。”

考虑到您的输入文件,我认为您有一些带有尾随空格的关键字名称

"catch       "
"false         "
在插入集合之前,可以使用boost::trim或您自己的trim修剪字符串以删除空格(请参见)

(如果您需要有关代码的建议:

  • 对于输入文件流,可以像这样使用std::ifstream:

    std::ifstream文件(“keywords.txt”)

  • 您不需要在作用域的任何位置调用.close(),它将自动完成,这要归功于

  • 您不应在任何情况下重复使用相同的std::string对象,您可以声明新的字符串对象以接近其用途。您应该给它们更好的名称,如“line”而不是“b”。这样做,您就不需要为字符串调用“.clear()”

  • 每行只有一个单词,您可以使用,而(fs>>b)这些>>将忽略空格(来自moldbinlo和wangxf注释)


)

考虑到您的输入文件,我认为您有一些带有尾随空格的关键字名称

"catch       "
"false         "
在插入集合之前,可以使用boost::trim或您自己的trim修剪字符串以删除空格(请参见)

(如果您需要有关代码的建议:

  • 对于输入文件流,可以像这样使用std::ifstream:

    std::ifstream文件(“keywords.txt”)

  • 您不需要在作用域的任何位置调用.close(),它将自动完成,这要归功于

  • 您不应在任何情况下重复使用相同的std::string对象,您可以声明新的字符串对象以接近其用途。您应该给它们更好的名称,如“line”而不是“b”。这样做,您就不需要为字符串调用“.clear()”

  • 每行只有一个单词,您可以使用,而(fs>>b)这些>>将忽略空格(来自moldbinlo和wangxf注释)



)

正确的缩进和变量名会有很大帮助。@Borgleader:是的,我为他做了,至少是代码缩进。由于关键字不能包含空格,
>
是一种比
getline
@molbdnilo更健壮的读取方式。是的,我尝试在其中使用fs>>像cin一样忽略空格。谢谢。正确的缩进和变量名会有很大帮助。@Borgleader:是的,我为他做了,至少是代码缩进。因为关键字不能包含空格,
>
是一种比
getline
@molbdnilo更健壮的读取方式。是的,我尝试在其中使用fs>>像cin一样忽略空格。谢谢。或者OP输入带有尾随空格的关键字,因此
keys.find()
失败。问题可能是使用了
std::getline
,它也读取尾随空格。op提供了输入文件什么是避免这个问题的好方法,我想用fscanf代替getline?或者对读取输入使用strtok()来提取其中的令牌?@RubixRechvin:
std::getline(stream,line);增压:微调(直线)应该工作得很好@RubixRechvin molbdnilo建议使用fs>>代替getline,效果很好。或者OP输入带有尾随空格的关键字,因此
keys.find()
失败。问题可能是使用了
std::getline
,它也读取尾随空格。op提供了输入文件什么是避免这个问题的好方法,我想用fscanf代替getline?或者对读取输入使用strtok()来提取其中的令牌?@RubixRechvin:
std::getline(stream,line);增压:微调(直线)应该工作得很好@RubixRechvin molbdnilo建议使用fs>>代替getline,而且效果很好。