我有一个单词数组,我有一个文本文件。我想要做的是使用单词数组并搜索文本文件,计算数组中每个单词出现在文本文件中的次数。
我曾考虑过使用 For 循环,但这只是给了我总字数,而不是每个字数。我无法将文本文件放入数组中,因为文本文件中有大约 40000 个单词。
计数后,我想将每个计数除以一个称为“比例”的整数值。然后用新的计数乘以一个字符串。
所以我目前正在这样做,如下所示。无论如何我可以提高效率吗?
任何帮助是极大的赞赏。
单词数组 = 测试词。
文件名 = testF。
inWord = 文件中的每个单词。
while(testF >> inWord)
{if (inWord == testwords[0]){
count1++;
}
if (inWord == testwords[1]){
count2++;
}
if (inWord == testwords[2]){
count3++;
}
if (inWord == testwords[3]){
count4++;
}
if (inWord == testwords[4]){
count5++;
}
if (inWord == testwords[5]){
count6++;
}
if (inWord == testwords[6]){
count7++;
}
if (inWord == testwords[7]){
count8++;
}
}
cout << testwords[0] << " " << count1 << " " << s1.append(count1/scale, '*') << endl;
cout << testwords[1] << " " << count2 << " " << s2.append(count2/scale, '*') << endl;
cout << testwords[2] << " " << count3 << " " << s3.append(count3/scale, '*') << endl;
cout << testwords[3] << " " << count4 << " " << s4.append(count4/scale, '*') << endl;
cout << testwords[4] << " " << count5 << " " << s5.append(count5/scale, '*') << endl;
cout << testwords[5] << " " << count6 << " " << s6.append(count6/scale, '*') << endl;
cout << testwords[6] << " " << count7 << " " << s7.append(count7/scale, '*') << endl;
cout << testwords[7] << " " << count8 << " " << s8.append(count8/scale, '*') << endl;