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;
    }
  • 相关阅读:
    错误机制
    IO文件
    lua与c的交互(运用)
    lua与c的交互(函数专用)
    string库
    元表

    模块与包
    zsh终端下,配置环境变量使用~/.zshrc
    MX150+python3.7+CUDA10.0+Tensorflow-gpu1.13安装记录
  • 原文地址:https://www.cnblogs.com/xiaokang01/p/12592816.html
Copyright © 2011-2022 走看看