C++ 使用stringstream和ifstream将用户输入与文件中的字进行比较
我正在写一个程序来比较用户输入的句子和文件中的单词。我想让程序告诉我是否在文件中找到了句子中的任何单词 我使用getline获取用户输入的句子,然后使用istringstream将句子切碎成单词,并将每个单词与文件中的每个单词进行比较。我的方法是使用while循环迭代文件中的每个单词,并将用户输入句子中的当前单词与文件中的当前单词进行比较。如果当前文件单词与当前句子单词相同,那么我会将bool设置为true,如果不是,那么我想跳到文件中的下一个单词,并将句子单词与下一个文件单词进行比较 我不知道如何让istringstream和ifstream跳转到while循环中的下一个单词。如何迭代到下一个单词 编辑:我需要在不使用向量、数组等的情况下执行此操作。我最初的方法是使用向量,但这是不允许的C++ 使用stringstream和ifstream将用户输入与文件中的字进行比较,c++,file,fstream,ifstream,istringstream,C++,File,Fstream,Ifstream,Istringstream,我正在写一个程序来比较用户输入的句子和文件中的单词。我想让程序告诉我是否在文件中找到了句子中的任何单词 我使用getline获取用户输入的句子,然后使用istringstream将句子切碎成单词,并将每个单词与文件中的每个单词进行比较。我的方法是使用while循环迭代文件中的每个单词,并将用户输入句子中的当前单词与文件中的当前单词进行比较。如果当前文件单词与当前句子单词相同,那么我会将bool设置为true,如果不是,那么我想跳到文件中的下一个单词,并将句子单词与下一个文件单词进行比较 我不知道
int main() {
string sentence;
string fileWord;
cout << "Input your sentence : " << endl;
getline(cin, sentence);
istringstream iss(sentence);
string sentenceWord;
ifstream adjectiveFile;
adjectiveFile.open("/project1.cpp/words/adjectives");
while(iss >> sentenceWord && adjectiveFile >> fileWord) {
if (sentenceWord == fileWord) {
cout << "The word " << sentenceWord << " is in the file" << endl;
bool adjectiveInSent = true;
} else {
}
}
adjectiveFile.close();
return 0;
}
intmain(){
串句;
字符串文件字;
cout语句(单词和形容词文件>>fileWord){
if(sentenceWord==fileWord){
您可以执行以下操作:
a)将文件中的单词存储在stl容器中
std::set
b) 阅读用户输入,并使用std::istringstream
现在,每个单词都用简单的“在A中形成的映射”来搜索,使用容器的<代码>查找< /C>方法。< /P> < P>一个可接受的C++解决方案应该是这样的:
int main() {
string sentence;
cout << "Input your sentence : " << endl;
getline(cin, sentence);
istringstream iss(sentence);
std::set<string> lookUpWords;
std::string word;
while(iss >> word) {
if(lookUpWords.find(word) != lookUpWords.end()) {
lookUpWords.insert(word);
}
}
ifstream adjectiveFile("/project1.cpp/words/adjectives");
string fileWord;
while(adjectiveFile >> fileWord) {
if (lookUpWords.find(fileWord) != lookUpWords.end()) {
cout << "The word " << sentenceWord << " is in the file" << endl;
}
}
adjectiveFile.close();
return 0;
}
这是一个只使用流的解决方案。正如评论员所指出的,这不是很有效,但老师就是老师,作业就是作业
该算法适用于复合词。它不会像简单的string.find()
那样匹配“fip”和“fipfop”
我在文件中使用istringstream
只是为了更容易测试。当然,您可以用ifstream
替换它
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
istringstream iss("bar fam fop fip");
istringstream adjectiveFile("foo bar baz fup fam fiz fipfop");
string fileWord, sentenceWord;
while( adjectiveFile >> fileWord ) {
iss.clear(); // clear EOF
iss.seekg( 0 ); // start from beginning
while( iss >> sentenceWord ) {
if (sentenceWord == fileWord) {
cout << "The word " << sentenceWord << " is in the file" << endl;
}
}
}
return 0;
}
你不能这样做。将整个文件读入std::set,然后在set中查找你从stringstream中提取的单词。什么是bool形容词insent=true;
应该做什么?稍后在程序中,我必须检查用户输入的句子是否以某种方式构造,即名词、动词、名词等。@NickKinlen没有带有作用域局部变量的laterz。如果你不能使用容器,你需要一个嵌套的while循环。外循环迭代文件中的单词,内循环迭代iss
。在内循环开始之前,每次调用iss.seek(0)
。映射键应该是什么?@πάνταῥεῖ 是的,我认为std::set
应该就可以了。嗯,另一种方法是??问题是我不能使用集合、数组、向量、任何库等等。@NickKinlen阅读了我对你的编辑的评论。这只是一个非常愚蠢的限制。@NickKinlen提供了另一种解决方案。sense.find()
不过对于复合词来说是不正确的。@zett42介意详细说明一下吗?你说的复合词是什么意思?这些词真的有问题吗?
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
istringstream iss("bar fam fop fip");
istringstream adjectiveFile("foo bar baz fup fam fiz fipfop");
string fileWord, sentenceWord;
while( adjectiveFile >> fileWord ) {
iss.clear(); // clear EOF
iss.seekg( 0 ); // start from beginning
while( iss >> sentenceWord ) {
if (sentenceWord == fileWord) {
cout << "The word " << sentenceWord << " is in the file" << endl;
}
}
}
return 0;
}