我有一個單詞的數組,我有一個文本文件。我想要做的就是使用單詞數組搜索文本文件,計算數組中每個單詞出現在文本文件中的次數。C++如何讓此代碼更高效?
我曾想過使用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;
強制性使用探查器評論:) – EvilTeach