首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统计多个单词的单词出现次数的问题

统计多个单词的单词出现次数的问题
EN

Stack Overflow用户
提问于 2017-02-20 18:14:05
回答 2查看 54关注 0票数 1

我遇到的问题是,我不太确定如何重置我的字数。我创建了一个单词搜索,但当我让它计算10个不同单词的出现次数时,它从它计数的第一个单词开始保持相同的数量。我认为我遇到的问题是在哪里使用了for循环

输出

代码语言:javascript
复制
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

它应该是什么样子

代码语言:javascript
复制
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

通过阅读我的代码,我确信我已经把它变得比原来更复杂了。我非常感谢我所做的任何建议和更正。

完整代码

代码语言:javascript
复制
#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;
}

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-20 23:53:10

您需要在外部循环的每次迭代开始时重置countpos

换句话说,改变这个:

代码语言:javascript
复制
size_t count = 0;
std::string::size_type pos = 0;
for (int i = 0; i < 10; i++)
{
    ...
}

要这样做:

代码语言:javascript
复制
for (int i = 0; i < 10; i++)
{
    size_t count = 0;
    std::string::size_type pos = 0;
    ...
}

顺便说一句,我也会将10更改为sizeof(word)/sizeof(*word)

票数 0
EN

Stack Overflow用户

发布于 2017-02-20 18:17:22

您使用word9而不是wordi,因此您将获得最后一个单词的结果,而不是每个单词的结果。尝试:

代码语言:javascript
复制
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";
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42341765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档