【问题标题】:How to calculate word frequency in a list using C++?如何使用 C++ 计算列表中的词频?
【发布时间】:2021-02-27 03:03:33
【问题描述】:

所以我在一个文件中有一些单词。我将它们读到一个列表中,然后我试图找到每个单词的频率。我的问题是我必须遵循某个不太灵活的列表实现。 这是 List 类:

const int maxListSize = 50;

template<class T>
class List {
private:
    int numberOfElements;
    int currentPosition;
    T data[maxListSize];
public:
    List() {
        numberOfElements = 0;
        currentPosition = -1;
    }
    void insert(T element) {
        if (numberOfElements >= maxListSize) {
            cout << "List is Full" << endl;
            return;
        }
        data[numberOfElements] = element;
        numberOfElements++;
    }

    bool first(T &element) {
        if (numberOfElements == 0) {
            cout << "List is Empty" << endl;
            return false;
        }
        else {
            currentPosition = 0;
            element = data[currentPosition];
            return true;
        }
    }

    bool next(T &element) {
        //Check if the user called the first function
        if (currentPosition < 0) {
            cout << "Please call the first function before calling the next" << endl;
            return false;
        }
        if (currentPosition >= numberOfElements - 1) {
            //cout << "No next item" << endl;
            return false;
        }
        currentPosition++;
        element = data[currentPosition];
        return true;
    }
};

假设我的列表被称为名称。如何获取每个单词的频率?

【问题讨论】:

  • 如果您可以使用给定的List 容器,那么您可以像List&lt;std::pair&lt;std::string, int&gt;&gt; names; 那样制作一个配对列表,当您获得新单词,如果它已经在列表中,则将其关联的int 值递增为计数器。

标签: c++ list c++11 c++14


【解决方案1】:

考虑到 List 的不幸界面,我会这样做。

最初我以为我会使用List&lt;pair&lt;string, int&gt;&gt;,但firstnext 函数提供了元素的副本,因此无法就地修改,所以是指针!

这会泄漏内存。如果不泄漏对您很重要,那么您可以使用智能指针或尝试释放内存。我认为简单可能会更好。

#include <iostream>
#include <string>

const int maxListSize = 50;

template<class T>
class List
{
private:
    int numberOfElements;
    int currentPosition;
    T data[maxListSize];
public:
    List()
    {
        numberOfElements = 0;
        currentPosition = -1;
    }
    void insert(T element)
    {
        if (numberOfElements >= maxListSize)
        {
            return;
        }
        data[numberOfElements] = element;
        numberOfElements++;
    }

    bool first(T &element)
    {
        if (numberOfElements == 0)
        {
            return false;
        }
        else
        {
            currentPosition = 0;
            element = data[currentPosition];
            return true;
        }
    }

    bool next(T &element)
    {
        if (currentPosition < 0)
        {
            return false;
        }
        if (currentPosition >= numberOfElements - 1)
        {
            return false;
        }
        currentPosition++;
        element = data[currentPosition];
        return true;
    }
};

using WordPair = std::pair<std::string, int>;
using WordList = List<WordPair*>;

void incrementCount(WordList &wl, const std::string& s)
{
    WordPair* item = nullptr;
    if (wl.first(item))
    {
        if (item->first == s)
        {
            ++(item->second);
            return;
        }
        while (wl.next(item))
        {
            if (item->first == s)
            {
                ++(item->second);
                return;
            }
        }
    }
    wl.insert(new WordPair { s, 1 });
}

void printList(WordList &wl)
{
    WordPair *item = nullptr;
    if (wl.first(item))
    {
        std::cout << item->first << " : " << item->second << "\n";
        while (wl.next(item))
        {
            std::cout << item->first << " : " << item->second << "\n";
        }
    }
}

int main()
{
    std::string words[10] = { "one", "two", "three", "four", "one", 
        "two", "three", "two", "three", "three" };
    WordList wl;
    for (int i = 0; i < 10; ++i)
    {
        incrementCount(wl, words[i]);
    }
    printList(wl);
}

示例:https://ideone.com/W4Slyq

【讨论】:

    【解决方案2】:

    您可以保留重复项吗?如果是这样,您可以遍历列表。

    int count(T &element) {
        int numberOfDuplicates = 0;
        for (int i = 0; i < numberOfElements; i++) {
            if (data[i] == element) {
                numberOfDuplicates++;
            }
        }
        return numberOfDuplicates;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-19
      • 2020-12-29
      • 1970-01-01
      • 2019-04-19
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      相关资源
      最近更新 更多