【发布时间】:2011-08-30 02:08:38
【问题描述】:
我有两个文本文件。其中一个包含大约 70,000 个名称(~1.5MB)的列表。另一个包含将从其他来源获得的文本。也就是说,每次执行程序时,这个文件的内容都会改变(~0.5MB)。本质上,我希望能够将一些文本粘贴到文本文件中,并查看我的列表中找到了哪些名称。有点像查找功能 (CTR + F),但有 70,000 个关键字。
无论如何,我目前所拥有的是:
int main()
{
ifstream namesfile("names.txt"); //names list
ifstream miscfile("misc.txt"); //misc text
vector<string> vecnames; //vector to hold names
vector<string> vecmisc; //vector to hold misc text
size_t found;
string s;
string t;
while (getline(namesfile,s))
veccomp.push_back(s);
while (getline(miscfile,t))
vectenk.push_back(t);
//outer loop iterates through names list
for (vector<string>::size_type i = 0; i != vecnames.size(); ++i) {
//inner loop iterates through the lines of the mist text file
for (vector<string>::size_type j = 0;j != vecmisc.size(); ++j) {
found=vecmisc[j].find(vecnames[i]);
if (found!=string::npos) {
cout << vecnames[i] << endl;
break;
}
}
}
cout << "SEARCH COMPLETE";
//to keep console application from exiting
getchar();
return 0;
}
现在,就提取我需要的数据而言,这非常有效,但是,它非常缓慢且显然效率低下,因为每个名称都要求我可能再次搜索整个文件,这给出了(75000 x # of lines in misc text file)迭代。如果有人可以提供帮助,我当然会很感激。一些示例代码是最受欢迎的。此外,如果这有什么不同,我正在使用 Dev C++。谢谢。
【问题讨论】:
标签: c++ performance string full-text-search