map的用法

1、lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)

upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)

例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2、而upper-bound(2)的话,返回的就是3

Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字,

#include <map>  
  
#include <string>  
  
#include <iostream>  
  
using namespace std;  
  
int main()  
  
{  
  
    map<int, string> mapStudent;  
  
    mapStudent[1] = "student_one";  
  
    mapStudent[3] = "student_three";  
  
    mapStudent[5] = "student_five";  
  
    map<int, string>::iterator iter;  
  
    iter = mapStudent.lower_bound(1);  
  
    //返回的是下界1的迭代器  
  
        cout<<iter->second<<endl;  
  
    iter = mapStudent.lower_bound(2);  
  
    //返回的是下界3的迭代器  
  
        cout<<iter->second<<endl;  
  
    iter = mapStudent.lower_bound(3);  
  
    //返回的是下界3的迭代器  
  
        cout<<iter->second<<endl;  
  
    iter = mapStudent.upper_bound(2);  
  
    //返回的是上界3的迭代器  
  
        cout<<iter->second<<endl;  
  
    iter = mapStudent.upper_bound(3);  
  
    //返回的是上界5的迭代器  
  
        cout<<iter->second<<endl;  
  
    pair<map<int, string>::iterator, map<int, string>::iterator> mappair;  
  
    mappair = mapStudent.equal_range(2);  
  
    if(mappair.first == mappair.second)  
  
        cout<<"Do not Find"<<endl;  
  
    else  
  
        cout<<"Find"<<endl;  
  
    mappair = mapStudent.equal_range(3);  
  
    if(mappair.first == mappair.second)  
  
        cout<<"Do not Find"<<endl;  
  
    else  
  
        cout<<"Find"<<endl;  
  
    return 0;  
}  

map中的swap用法

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值