map容器,下标行为的编程意义
2011-12-07 19:28
357 查看
关联容器与顺序容器的本质差别在于:一个是按键存储和访问元素,另一个是按元素在容器中的位置顺序存储和读取元素。
在map容器中,如果下标表示的键不存在则创建新元素,这一特性对于统计单词的频率非常简单!
在map容器中,如果下标表示的键不存在则创建新元素,这一特性对于统计单词的频率非常简单!
#include<iostream> #include<map> #include<algorithm> #include<vector> #include<string> using namespace std; int main() { map<string,int> word_count; //word_count用来记录单词出现的次数 vector<string> words; cout<<"Enter some words:"<<endl; string word; while(cin>>word) { //如果该键不存在,则将它放入容其中 if( find(words.begin(),words.end(),word)==words.end() ) words.push_back(word); ++word_count[word]; } cout<<endl<<"The word are:"<<endl; for(vector<string>::iterator it=words.begin();it!=words.end();++it) cout<<*it<<": "<<word_count[*it]<<endl; return 0; }
Enter some words: hot tokyo bath angel lip lip hot bath The word are: hot: 2 tokyo: 1 bath: 2 angel: 1 lip: 2
相关文章推荐
- Java编程的逻辑 (75) - 并发容器 - 基于SkipList的Map和Set
- STL容器map的下标运算符[]
- map这种容器的下边访问和Vector等容器的下标访问有本质的区别。
- STL容器map的下标运算符[]
- STL容器map的下标运算符[]
- 关联容器map的下标操作(特殊)---map的本质就是关联数组, 数组的本质就是映射!
- 关联容器-map与pair的使用(续)
- STL容器之MAP操作
- C++中map容器实现单词转换的程序
- 几种常见容器的比较:vector,map,hashmap,list,set
- 几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table
- Java 并发编程深入学习——CopyOnWrite容器使用和原理分析
- cocos2dx的模板容器简单使用(Vector,Map,Value)
- [C++ 从入门到放弃-05]C++STL之map映照容器
- Hibernate三种容器(List,Set,Map)映射技术之Map映射
- C++各个容器比较(vector,deque,list,set,map,queue,stack)
- 【C/C++】关联容器pair map set
- map容器的insert用法总结
- STL源码解析-05关联容器-01map&set
- linux下C编程技巧:linux man开发手册安装以及使用意义