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

bob*_*mac 1 c++ vector

我想获得存储在矢量中的单词的频率.我已多次用Google搜索我的问题,而不是去找对我有用的东西.我找到了一个网站,有人说使用该unique命令来计算单词的频率,但我找不到任何关于如何完成这个的例子.

Kon*_*lph 6

使用a 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";
}
Run Code Online (Sandbox Code Playgroud)