私の問題は、単語カウントをリセットする方法が正確ではないということです。私は単語検索を作成しましたが、10個の異なる単語の出現数を数えれば、それは最初の単語から同じ数のままです。私はfor
ループ複数の単語の単語の出現数をカウントする問題
出力
boy appeared 3 times
Snape appeared 3 times
Dumbledore appeared 3 times
he appeared 3 times
her appeared 3 times
the appeared 3 times
it appeared 3 times
is appeared 3 times
will appeared 3 times
all appeared 3 times
それは私のコードを読み取ることで
boy appeared 3 times
Snape appeared 7 times
Dumbledore appeared 4 times
he appeared 27 times
her appeared 4 times
the appeared 13 times
it appeared 6 times
is appeared 12 times
will appeared 2 times
all appeared 3 times
のようになります。私は私がきたと確信しているを使用します。ここで、私がいる問題であると考えていますそれはそれをより複雑にしました。私が作ったアドバイスや修正は感謝しています。
全コード事前に
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
// Main Function
int main()
{
// Declaration
std::string list, passage, word[10];
std::ifstream listFile("WordList.txt", std::ios::in);
std::ifstream passageFile("HarryPotterPassage.txt", std::ios::in);
std::vector<std::string> vec_wordList, vec_passage;
/* Read a file that contains a list of 10 words */
if (listFile.is_open())
{
// Store text file in a vector
while (listFile)
{
listFile >> list;
vec_wordList.push_back(list);
}
// Assign vector to individual strings
for (int i = 0; i < 10; i++)
{
word[i] = vec_wordList[i];
}
// Close file
listFile.close();
}
else
std::cout << "No file found.\n";
/* Read another file containing a paragraph */
if (passageFile.is_open())
{
while (passageFile)
{
// Store text file in a string
std::getline(passageFile, passage);
}
// Close file
passageFile.close();
}
else
std::cout << "No file found.\n";
//std::cout << passage << '\n';
/* Count the number of words from the first file
from the second file that contains the paragraph */
size_t count = 0;
std::string::size_type pos = 0;
for (int i = 0; i < 10; i++)
{
while ((pos = passage.find(word[i], pos)) != std::string::npos)
{
count++;
pos += word[i].size();
}
std::cout << word[i] << " appeared " << count << " many times\n";
}
system("pause");
return 0;
}
感謝。
おそらく、 'std :: unoredered_map'を使って解決するほうが簡単でしょう。 –
私は 'for = 'ループの各繰り返しの始めに' count = 0'と 'pos = 0'を設定する必要があると思います。基本的には、これらの2つの宣言を**そのループに移動することができます。 –