统计单词频率-map

从这里看到的博客,还有一些看不懂的地方待求证。

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
    map<string, int> st;

    string key;
    int cnt = 0;
    while(cin >> key)
    {
        st[key]++; // 这里不明白
        cnt++;
    }

    cout.setf(ios::fixed);  // 以定点形式表示浮点数
    cout.precision(4);      // 设置小数部分的有效数字

    map<string, int>::const_iterator it;
    for (it = st.begin(); it != st.end(); ++it)
    {
        double rate = 100*(double) it->second/cnt;
        cout << it->first << " "<< rate <<"%"<<endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/devillixin/article/details/51312390