【问题标题】:How to find the most repeated word?如何找到重复次数最多的单词?
【发布时间】:2018-11-29 17:13:29
【问题描述】:

如何让程序打印出重复次数最多的单词?现在它打印如下:

输入: 苹果香蕉苹果苹果

输出:

苹果出现了 3 次

香蕉出现了 1 次

int main() {
        string words;
        vector<string> stringHolder;

        while (cin >> words)
        {
            stringHolder.push_back(words);
        }
        sort(stringHolder.begin(), stringHolder.end());

        int vSize = stringHolder.size();
        if (vSize == 0) {
            cout << " no words ";
        }
        int wordCount = 1;
        words = stringHolder[0];
        for (int i = 1; i<vSize; i++) {
            if (words != stringHolder[i]) {
                cout << words << " appeared " << wordCount << " times" << endl;
                wordCount = 0;
                words = stringHolder[i];
            }
            wordCount++;
        }
        cout << words << " appeared " << wordCount << " times" << endl;
        system("pause");
    }

【问题讨论】:

  • 我建议使用std::map&lt;string,int&gt; 来跟踪单词计数器。
  • std::map确实是最简单的解决方案。如果由于特定原因(作业...)不允许您使用它,请对数组进行排序stringHolder,然后使用单个for 循环以线性方式计数
  • 已经有排序功能
  • @πάνταῥεῖ 如何使用 std::map 将单词添加到地图中。我无法使用 map::insert

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


【解决方案1】:

我会为此使用std::mapstd::set

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>

// typedef for a "word => count" map
using wordcountmap = std::map<std::string, int>;
// typedefs for making a sorted set
using wordcountpair = std::pair<std::string, int>;
using CompareFunc = bool(*)(const wordcountpair&, const wordcountpair&);
using wordcountset = std::set<wordcountpair, CompareFunc>;

wordcountmap make_wordcountmap(const std::vector<std::string>& words)
{
    wordcountmap wcm;
    // map::operator[] is used to access or
    // insert specified element
    for(const std::string& word : words) {
        // std::string has a std::hash specialization
        // https://en.cppreference.com/w/cpp/utility/hash
        // calculating the hash for you
        //
        // If "word" does not exist in the map, it will be inserted
        // with a default constructed value, which is 0 for an int.
        ++wcm[word];
    }
    return wcm;
}

wordcountset make_wordcountset(const wordcountmap& wcm) {
    // populate the set using the maps iterators and provide a comparator
    // lambda function that will sort the set in descending order
    wordcountset wcs(
        wcm.begin(),
        wcm.end(),
        [](const wordcountpair& a, const wordcountpair& b) {
            return b.second==a.second ? b.first<a.first : b.second<a.second;
        }
    );
    return wcs;
}

int main(int argc, char* argv[]) {
    // put all command line arguments in a string vector
    std::vector<std::string> args(argv+1, argv+argc);

    // Your "word => count" map
    wordcountmap MyWCM = make_wordcountmap(args);

    // create a set of <word, count> pairs, sorted in count order.
    wordcountset set_of_sorted_words = make_wordcountset(MyWCM);

    // output the number of times the words appeared
    for(const wordcountpair& element : set_of_sorted_words) {
        // element.first  is the "word" in the wordcountpair
        // element.second is the "value" in the wordcountpair

        std::cout << element.second << " " << element.first << "\n";
    }

    if(set_of_sorted_words.size())
        std::cout << "The winner is " << (*set_of_sorted_words.begin()).first << "\n";

    return 0;
}

输出:

3 apple
1 banana
The winner is apple

【讨论】:

  • 谢谢,但 Visual Studio 给出错误:“调试断言失败”,表达式:map/set iterator not dereferencable。还有,main() 里面的东西有什么作用?
  • 还有更简单的方法吗?您的代码看起来太高级了。
  • @Jemi 当你测试这个时,你是否将这些单词作为命令行参数传递?以下期望单词是命令行参数(而不是使用 cin 读取):std::vector&lt;std::string&gt; args(argv+1, argv+argc);
  • 听起来很不错。它也运行程序吗?您使用的是哪个在线编译器? (在我们说话的时候开始将 VS2017 升级到最新版本 - 它可能会马上解决这个问题)
  • 我刚刚用 Visual Studio 2017 测试了这段代码,它运行良好。我在属性->调试->命令参数中输入了以下内容:apple banana apple apple,然后按调试。我还在 main 的最后一行放了一个断点,让它在程序完成时不关闭窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多