首页 > 其他 > 详细

map统计单词个数

时间:2020-03-29 16:47:18      阅读:70      评论:0      收藏:0      [点我收藏+]

用map统计字符串中每个字符出现的次数

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

using namespace std;

// map统计字符出现的次数
int main(int argc, char **argv)
{
    string str = "slskdflsafd";
    map<char, int>map1;
    for(int i = 0; i < str.size(); i++)
    {
        map1[str[i]]++;
    }
    for(auto it = map1.begin(); it != map1.end(); it++)
    {
        cout << it->first <<":"<<it->second << endl;
    }
    return 0;
}

 

统计单词出现的个数

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

int main(int argc, char **argv)
{
    vector<string> vec(0);
    int N;
    cout << "输入单词个数:";
    cin >> N;
    cout << "输入单词:" << endl;
    while(N--)
    {
        string str;
        cin >> str;
        vec.push_back(str);
    }
    map<string, int>map1;
    for(int i = 0; i < vec.size(); i++)
    {
        map1[vec[i]]++;
    }
    for(auto it = map1.begin(); it != map1.end(); it++)
    {
        cout << it->first <<":"<<it->second << endl;
    }
    return 0;
}

 

map统计单词个数

原文:https://www.cnblogs.com/xiaokang01/p/12592816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!