STL

STL是Standard Template Library的简称。STL是一些“容器”的集合,STL也是算法和其他一些组件的集合。

关联式容器

【映射map】   #include<map>

功能

一、自动建立key-value的对应,他们分别可以是你任意需要的类型。

二、根据key的值快速查找、快速插入key-value、快速删除记录、根据key修改value、遍历所有。

使用

std:map<int,string> personnel;   

map<int.string> personnel;

函数

基本函数

 begin()         返回指向map头部的迭代器

clear()        删除所有元素

count()         返回指定元素出现的次数

empty()         如果map为空则返回true

end()           返回指向map末尾的迭代器

equal_range()   返回特殊条目的迭代器对

erase()         删除一个元素

find()          查找一个元素

get_allocator() 返回map的配置器

 insert()        插入元素

key_comp()      返回比较元素key的函数

lower_bound()   返回键值>=给定元素的第一个位置

max_size()      返回可以容纳的最大元素个数

rbegin()        返回一个指向map尾部的逆向迭代器

rend()          返回一个指向map头部的逆向迭代器

size()          返回map中元素的个数

swap()           交换两个map

upper_bound()    返回键值>给定元素的第一个位置

value_comp()     返回比较元素value的函数

insert 插入函数

//插入pair数据 

map<int, string> mapStudent;  
mapStudent.insert(pair<int, string>(1, "student_one"));  
mapStudent.insert(pair<int, string>(2, "student_two"));  
mapStudent.insert(pair<int, string>(3, "student_three"));  

//用数组方式插入数据
 map<int, string> mapStudent;  
 mapStudent[1] = "student_one";  
 mapStudent[2] = "student_two";  
 mapStudent[3] = "student_three"; 

虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的。第二种,它可以覆盖以前该关键字对应的值。

mapStudent.insert(map<int, string> (1, "student_one"));

mapStudent.insert(map<int, string>(1, "student_two"));

比如,上面两条语句,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效。

map<int, string> mapStudent;   

mapStudent[1] = "student_one";  

mapStudent[1] = "student_two"; 

上面是数组插入覆盖效果。

通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
map<int, string> mapStudent;  
pair<map<int, string>::iterator, bool> Insert_Pair;  
Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));  
if(Insert_Pair.second == true)  
    cout<<"Insert Successfully"<<endl;  
else  
   cout<<"Insert Failure"<<endl;  

size大小函数

int nSize = mapStudent.size();   //获得插入数据的是多少

earse移除函数

map<int, string> mapStudent;  
mapStudent.insert(pair<int, string>(1, "student_one"));  
  
//用迭代器删除  
map<int, string>::iterator iter;  
iter = mapStudent.find(1);  
mapStudent.erase(iter);  

//用key删除  如果删除了会返回1,否则返回0  
int n = mapStudent.erase(1);

swap交换函数

map中的swap不是一个容器中的元素交换,而是两个容器所有元素的交换。

sort排序函数

map中的元素是自动按Key升序排序,所以不能对map用sort函数;

 

 

迭代器--数据的遍历

前向迭代器

map<int, string>::iterator iter;   
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;  
 

反相迭代器

map<int, string>::reverse_iterator iter;  
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)  
cout<<iter->first<<"  "<<iter->second<<endl; 


数组遍历

for(int nindex = 1; nindex <= nSize; nindex++)  //从1开始
     cout<<mapStudent[nindex]<<endl;  
  

查找

第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系(map中不存在相同元素,所以返回值只能是1或0),就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了

map<string, int> test;
test.insert(make_pair("test1", 1));
test.insert(make_pair("test2", 2));

cout<<"coout 0:"<<test.count("test0")<<endl;       输出coout 0:0
cout<<"coout 1:"<<test.count("test1")<<endl;       输出coout 1: 1

第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。

map<string, int> test;
test.insert(make_pair("test1", 1));
test.insert(make_pair("test2", 2));

map<string, int> ::iterator it;
	
it = test.find("test0");

if (it == test.end())    // test.end()是空的
	cout << "test0 not found" << endl;
else 
	cout << it->second << endl;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值