我可以使用c++中的unique命令来获取频率吗

can i use the unique command in c++ to get frequency

本文关键字:获取 频率 命令 unique 可以使 c++ 中的 我可以      更新时间:2023-10-16

我想得到存储在向量中的单词的频率。我已经在谷歌上搜索了很多次我的问题,但没有去做对我有用的事情。我发现了一个网站,有人说要使用unique命令来计算单词的频率,但我找不到任何例子来说明如何做到这一点。

使用map<string, unsigned>创建直方图

using std::string;
using std::map;
using std::vector;
typedef map<string, unsigned> counts_t;
// Create the histogram
counts_t histogram;
for (vector<string>::const_iterator i = vec.begin(); i != vec.end(); ++i)
    ++histogram[*i];
// ... and display it.
for (counts_t::const_iterator i = histogram.begin(); i != histogram.end(); ++i) {
    double freq = static_cast<double>(i->second) / vec.size();
    std::cout << i->first << ": " << freq << "n";
}

不使用unique,但对于单词计数来说,无论是在内存使用方面还是在其他方面,都很难击败trie或其任何衍生物;速度