多个单词的计数单词出现的问题
Issue with counting word occurrence for multiple words
我遇到的问题是我不确定如何重置我的单词计数。我创建了一个单词搜索,但是当我计算出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 timesn";
}
system("pause");
return 0;
}
预先感谢。
您使用word [9]而不是单词[i],因此您可以获得最后一个单词的结果,而不是每个单词的结果。尝试:
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 timesn";
}
您需要在外部循环的每次迭代开始时重置count
和pos
。
换句话说,更改以下方式:
size_t count = 0;
std::string::size_type pos = 0;
for (int i = 0; i < 10; i++)
{
...
}
:
for (int i = 0; i < 10; i++)
{
size_t count = 0;
std::string::size_type pos = 0;
...
}
顺便说一句,我还将10
更改为sizeof(word)/sizeof(*word)
。
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 算法问题:查找从堆栈中弹出的所有序列
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 当字符串是某个单词时给出输出?
- 阶乘问题在 c++ 中给出错误的输出
- C++泛型类错误,问题出在哪里?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 在Hackerearth上获取运行时错误(SIGSEGV),无法找出代码中的问题
- 不确定出了什么问题(字符串输入)
- 我无法找出我的代码出了什么问题
- 我想知道变焦和轨道在哪里出了问题
- 字数统计函数在将单词添加到一组唯一单词时遇到问题
- 在文本中查找半个单词"ABBA"的问题
- 向量<对<双精度,双精度>>给出问题
- 服务器未收到UDP数据包,花费数小时试图找出问题所在
- C++ 输出找出问题
- 在成功运行10小时后,在发布模式下从windows应用程序中获得异常,如何检测出问题
- 额外的单词出现在visualc++输出窗口