zoukankan      html  css  js  c++  java
  • map统计单词个数

    用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;
    }
  • 相关阅读:
    CSS之各种居中
    三步教会你装系统
    65条最常用正则表达式
    MongoDB介绍
    MongoDB基本命令用
    log4j配置
    使用spring + ActiveMQ 总结
    log4j配置文件
    如何入侵局域网电脑
    目标检测的图像特征提取
  • 原文地址:https://www.cnblogs.com/xiaokang01/p/12592816.html
Copyright © 2011-2022 走看看