c++_map_初探1
来源:互联网 发布:美图app软件 编辑:程序博客网 时间:2024/06/05 19:58
1. 统计单词个数。
#include<iostream>#include<map>using namespace std;int main(int argc, char const *argv[]){ string word=""; map<string,int> word_count; while(cin>>word) ++word_count[word]; for(const auto &w:word_count) cout<<w.first<<"出现了"<<w.second<<"次"<<endl; return 0;}
这里面的word_count容器被用来统计单词出现的次数。
what is your is what^Zis出现了2次what出现了2次your出现了1次--------------------------------Process exited with return value 0Press any key to continue . . .
2. 利用map统计家庭和孩子
#include<iostream>#include<map>#include<string>#include<vector>using namespace std;void add_family(map<string,vector<string>> &families,const string &family){ if(families.find(family)!=families.end()) families[family]=vector<string>();} void add_child(map<string,vector<string>> &families,const string &family,const string &child){ families[family].push_back(child);}int main(int argc, char const *argv[]){ map<string,vector<string>> families; add_family(families,"xu"); add_child(families,"xu","yuan"); add_child(families,"xu","yicheng"); add_child(families,"wei","zhuang");// add_family(families,"wei"); for(auto f:families) { cout<<f.first<<"家有:"; for(auto s:f.second) cout<<s<<" "; cout<<endl; } return 0;}
输出结果:
wei家有:zhuangxu家有:yuan yicheng--------------------------------Process exited with return value 0Press any key to continue . . .
阅读全文
0 0
- c++_map_初探1
- c++_vector_初探1
- lua 与 c/c++ 交互(1) 初探
- 初探Object-c 1:类的初步
- C++_类_初探1
- C++_字符串_初探1
- select初探(c/c++)
- C#网络编程初探
- C#网络编程初探
- C#网络编程初探
- C#网络编程初探
- C语言指针初探
- C#网络编程初探
- 初探C++(第一步)
- C++_STL_stack_初探
- rabbitmq-c初探
- C Socket初探
- C Socket初探
- JIRA如何统计重新打开的BUG及BUG数
- 添加
- 开机启动Tomcat以及隐藏运行窗口
- Creating Practical Scripts Part IV
- Kotlin报错统计
- c++_map_初探1
- shell变量及环境变量配置文件详解
- cocoaPods升级遇到的问题 以及升级ruby 升级cocoaPos 整理
- Collect
- javascript中Object实例训练一
- 安装crontab
- 通过ApplicationContextAware加载Spring上下文环境
- bzoj 1237: [SCOI2008]配对(DP)
- Session详解