处理将参数与文件中的文本进行比较的程序(我的文件是包含大量英语单词的字典)。比较由单个字符的两个字符串C++
现在应用程序只工作字符串完全匹配。
想知道是否有一种方法来比较输入到文件中完整字符串的部分字符串,并将其匹配。 例如,如果参数是ap,它会匹配它到苹果,应用程序联盟分机。
# 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'匹配吗?然后只是比较第一个字符。否则,[这里](http://stackoverflow.com/questions/1878001/how-do-i-check-if-ac-string-starts-with-a-certain-string-and-convert-a-sub)是你的副本。而这个'eof'的东西...不要这样做,谷歌为什么/做什么,而不是。 – LogicStuff
std :: string :: compare()或std :: regex - 不要在eof()上循环 - 请参阅https://latedev.wordpress.com/2012/12/04/all-about-eof/为什么 –
也许你正在通过像[lenshtein-distance](https://en.wikipedia.org/wiki/Levenshtein_distance)这样的algorythm寻找相似之处? [Google有链接](http://www.google.com/search?q=levenshtein+distance+c%2B%2B) – LotPings