我有两个文本文件。其中包含大约 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 杂项文本文件中的行数)迭代。如果有人可以提供帮助,我一定会很感激。一些示例代码是最受欢迎的。此外,如果这有什么区别的话,我正在使用 Dev C++。谢谢。
Use a std::hash_set。将所有关键字插入集合中,然后遍历大型文档,每次遇到一个单词时,测试集合中是否包含该单词。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)