【发布时间】:2016-12-10 23:13:42
【问题描述】:
正在开发一个将参数与文件中的文本进行比较的程序(我的文件是一个包含很多英文单词的字典)。
目前,应用程序仅在完全匹配的字符串中工作。
想知道是否有一种方法可以将输入的部分字符串与文件中的完整字符串进行比较并使其匹配。 例如,如果 arg 是 ap,它会匹配到 apple, application Alliance ext。
# include <iostream>
# include <fstream>
# include <cstdlib>
# include <string>
using namespace std;
int main ( int argc, char *argv[] ) {
ifstream inFile;
inFile.open("/Users/mikelucci/Desktop/american-english-insane");
//Check for error
if (inFile.fail()) {
cerr << "Fail to Open File" << endl;
exit(1);
}
string word;
int count = 0;
//Use loop to read through file until the end
while (!inFile.eof()) {
inFile >> word;
if (word == argv[1]) {
count++;
}
}
cout << count << " words matched." << endl;
inFile.close();
return 0;
}
【问题讨论】:
-
ap应该与alliance匹配吗?然后只比较第一个字符。否则,here 是您的副本。而那个eof的事情......不要这样做,而是用谷歌搜索为什么/做什么。 -
std::string::compare() 或 std::regex - 不要在 eof() 上循环 - 请参阅 latedev.wordpress.com/2012/12/04/all-about-eof 了解原因
-
也许您正在通过像the lenshtein-distance 这样的算法来寻找相似之处? Google has links