map(STL)
来源:互联网 发布:java一般控件 编辑:程序博客网 时间:2024/06/03 19:12
•问题1:输入一些字符串,统计每个字符串出现的次数。
•egg abc egga abc qwer asdf egg abc
•abc : 3
•egg : 2
•qwer: 1
•
•问题2:输入一些数字。统计每个数字出现的次数。
•1000000000, 123, 2, 4125246, 2
(hash音译为“哈希”,又成为预映射)
**map容器的简单运用
**统计单词出现的频率
*/
#include"head.h"
int main()
{
map< string, int > map_score;
/*使用下标访问 map 对象
**如果在map中找不到这个键值,则插入这个新键
**如果在map中找到了这个键值,则只更新键所对应的值 */
//map_score["kity"] = 23.43f;
//map_score["kity"] = 23.11f;
//map_score["andy"] = 23.22f;
//cout<< map_score["andy"] << endl;
//下标操作返回左值
string word;
while( cin >> word )
{
//++map_score[word];
//返回true表示该键不存在map中
//返回false表示该键存在map中
pair< map< string, int >::iterator, bool > pa = map_score.insert( make_pair( word, 1 ) );
//如果,该键存在,则只更新数量
if( !pa.second ) { ++pa.first->second; } }
map< string, int >::iterator map_it = map_score.find( "love" );
if( map_it != map_score.end() )
cout << "存在该键值" << endl; else cout << "不存在该键值" << endl;
//删除指定键值的元素,只返回0(不存在该键值,删除失败)和1(删除成功)
if( map_score.erase( "love" ) )
cout << "删除成功." << endl;
else cout << "不存在该键值" << endl;
//map 迭代器进行解引用将产生 pair 类型的对象
map< string, int >::const_iterator map_iter = map_score.begin();
//遍历所有的元素
for( ; map_iter != map_score.end(); map_iter++ )
{
cout << map_iter->first << ":" << map_iter->second << endl;
}
return 0;
}
•map //做hash,第一个键值必须是定义了 < 的数据类型(例如 string ,pair,int,char)
•头文件<map>
•定义 map<string, int> m;
•初始化 m.clear();
•逗号分开的是两个数据,第一个是键值,第二个是键值对应的值。
•赋值 m["abc"] = 1;
•访问m["abc"];
•查找某一个键值x是否在m中 m.count(x)
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的
/***map容器的简单运用
**统计单词出现的频率
*/
#include"head.h"
int main()
{
map< string, int > map_score;
/*使用下标访问 map 对象
**如果在map中找不到这个键值,则插入这个新键
**如果在map中找到了这个键值,则只更新键所对应的值 */
//map_score["kity"] = 23.43f;
//map_score["kity"] = 23.11f;
//map_score["andy"] = 23.22f;
//cout<< map_score["andy"] << endl;
//下标操作返回左值
string word;
while( cin >> word )
{
//++map_score[word];
//返回true表示该键不存在map中
//返回false表示该键存在map中
pair< map< string, int >::iterator, bool > pa = map_score.insert( make_pair( word, 1 ) );
//如果,该键存在,则只更新数量
if( !pa.second ) { ++pa.first->second; } }
map< string, int >::iterator map_it = map_score.find( "love" );
if( map_it != map_score.end() )
cout << "存在该键值" << endl; else cout << "不存在该键值" << endl;
//删除指定键值的元素,只返回0(不存在该键值,删除失败)和1(删除成功)
if( map_score.erase( "love" ) )
cout << "删除成功." << endl;
else cout << "不存在该键值" << endl;
//map 迭代器进行解引用将产生 pair 类型的对象
map< string, int >::const_iterator map_iter = map_score.begin();
//遍历所有的元素
for( ; map_iter != map_score.end(); map_iter++ )
{
cout << map_iter->first << ":" << map_iter->second << endl;
}
return 0;
}
0 0
- STL-map
- stl -map
- stl map
- STL Map
- STL---map
- STL(MAP)
- STL(map)
- STL--map
- STL map
- stl map
- STL map
- STL----map
- stl map
- STL map
- 【STL】map
- STL--map
- STL map
- STL map
- Mac系统下Homebrew的安装和使用Homebrew安装python
- 指定时间为0,9OAL 就直接执行wait(0)
- UVA112
- 使用<em><em>
- soj2669序关系
- map(STL)
- 生产者和消费者只能notify/wait实现?你弱爆了
- storm学习三 drpc学习
- hdu3853(概率dp)
- 计算机经典书籍电子书合集(适合计算机学生学习以及程序员笔试、面试)
- QT学习资料
- KJFrameForAndroid框架学习----高效加载Bitmap
- hdu 4862 费用流
- python的浅拷贝,深拷贝和赋值