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不是一个容器中的元素交换,而是两个容器所有元素的交换。