C++学习笔记——map与set的组合使用
来源:互联网 发布:尼古丁的好处 知乎 编辑:程序博客网 时间:2024/05/17 21:32
map在单词计数程序的应用
map:关联数组;保存关键字——值对
#include<iostream>#include<string>#include<map>using namespace std;int main(){ map<string,size_t> word_count; string word; while(cin>>word) {/* istringstream a; a.str(word); char b; a>>b; cout<<b; if(b=='\r') { cout<<"1"; break; }*/ ++word_count[word]; } for(const auto &w:word_count) cout<<w.first<<" occurs "<<w.second<<((w.second>1?" times":" time"))<<endl; system("pause"); return 0;}
这里有一个问题,至今未解决,就是回车不能跳出for循环,暂时用Ctrl+Z或者F6来强行跳出while
结果
w是对word_count的引用
size_t 是一种机器相关的无符号类型,它被设计的足够大以便能表示内存中任意对象的大小。在cstdef头文件中定义了size_t类型
set:关键字即值,即只保存关键字的容器
#include<iostream>#include<string>#include<map>#include<set>using namespace std;int main(){ map<string,size_t> word_count; set<string> exclude ; //exclude.insert{"The","But","And","Or","An","A","the","but","and","or","an","a"}; exclude.insert("The"); string word; while(cin>>word) { if(exclude.find(word)==exclude.end()) ++word_count[word]; } for(const auto &w:word_count) cout<<w.first<<" occurs "<<w.second<<((w.second>1?" times":" time"))<<endl; system("pause"); return 0;}
此外还应补充的一点是 map 与 set 的关键字都是不能改变的
1 0
- C++学习笔记——map与set的组合使用
- map与set的使用
- set /list/ map 使用 学习笔记
- Spring学习笔记 关于Bean属性的初始化 - 使用inner bean以及List, Map与Set的初始化
- List, Map, Set与Iterator的使用
- map与set容器的使用
- Map与Set的使用little tips
- map与set的原理及使用
- C++学习笔记(五):关联容器--map与set
- java学习个人笔记---容器之Map与Set关系
- 疯狂Java学习笔记(43)-----------Map,List与Set的区别
- C++学习笔记之——map set 与 opencv中的数据结构 如 point 等不兼容
- (c++)vector map multimap set 的使用
- C++map和set的简单理解和使用案例
- 【C++】学习笔记十二——类型组合
- set/multiset,map/multimap的使用与比较+pair介绍
- leetcode 第三题:标准容器set与map的使用
- C++中map和set的使用与区别
- TIANKENG’s restaurant(贪心)
- 【杭电4883】TIANKENG’s restaurant
- Mac brew安装mysql之后无法启动mysql
- Android中使用Bitmap类将矩形图片转为圆形的方法
- Spark Transformation —— mapPartitions
- C++学习笔记——map与set的组合使用
- Java NIO使用及原理分析(三)
- 使用IntelliJ IDEA 15和Maven 开发Spring Data JPA + MVC Web应用
- define,const与inlin
- tjut 4882
- 并查集——求无向图的所有连通子图
- HDU 3709 Balanced Number
- ART世界探险(4) - 数据传送指令和桶型移位器
- Find MaxXorSum 字典树+DP