1、什么是map?
映射。
可以多对一。 但是不可以一对多
可用< int , char > < int ,string> < string, int >
2、map的优势
1、根据key值快速查找记录,查找的复杂度基本是Log(N)
2、快速插入Key -Value 记录。
3、快速删除记录
4、根据Key 修改value记录。
3、如何插入值
方法一(pair结构)
#include<bits/stdc++.h>
using namespace std;
int main(){
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>::iterator it;
for(it = mapStudent.begin(); it != mapStudent.end(); it++) {
printf("%d\t",it->first);
cout<<it->second;
cout<<endl;
}
return 0;
}
方法二(value_type)
#include<bits/stdc++.h>
using namespace std;
int main(){
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(1, "student one"));
mapStudent.insert(map<int, string>::value_type(2, "student two"));
mapStudent.insert(map<int, string>::value_type(3, "student three"));
map<int, string>::iterator it;
for(it = mapStudent.begin(); it != mapStudent.end(); it++) {
printf("%d\t",it->first);
cout<<it->second;
cout<<endl;
}
return 0;
}
方法三(数组)
#include<bits/stdc++.h>
using namespace std;
int main(){
map<string , int > mapStudent;
mapStudent["student one"] = 1;
mapStudent["student two"] = 2 ;
mapStudent["student three"] = 3;
map<string,int >::iterator it;
for(it = mapStudent.begin(); it != mapStudent.end(); it++) {
cout<<it->first<<endl;
cout<<it->second;
cout<<endl;
}
return 0;
}
4、验证插入成功
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
pair< map<int, string>::iterator , bool> Insert_Pair;
ma[1] = "student_e";
Insert_Pair = ma.insert(pair<int, string>(1, "student_one"));
if(Insert_Pair.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl;
ma[4] = "student_two";
Insert_Pair = ma.insert(pair<int, string>(2, "student_two"));
if(Insert_Pair.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl;
map<int, string>::iterator iter;
for(iter = ma.begin(); iter != ma.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
}
5、强制覆盖方法
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
ma[1] = "student_1";
ma[1] = "student_2";
ma[2] = "student_3";
map<int, string>::iterator iter;
for(iter = ma.begin(); iter != ma.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
return 0;
}
6、map大小函数.size()
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
ma[1] = "student_1";
ma[1] = "student_2";
ma[2] = "student_3";
map<int, string>::iterator iter;
for(iter = ma.begin(); iter != ma.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
int n;
n = ma.size() ;
printf("size = %d\n",n);
return 0;
}
7、map的遍历方法
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
ma[1] = "student_1";
ma[2] = "student_2";
ma[3] = "student_3";
map<int, string>::iterator iter1;
map<int, string>::reverse_iterator iter2;
int n;
n = ma.size() ;
printf("size = %d\n",n);
printf("正向:\n");
for(iter1 = ma.begin(); iter1 != ma.end(); iter1++)
cout<<iter1->first<<' '<<iter1->second<<endl;
printf("正向:\n");
for(int i = 1; i <= n; i++)
cout<<i<<" "<<ma[i]<<endl;
printf("反向:\n");
for(iter2 = ma.rbegin() ; iter2 != ma.rend() ;iter2++)
cout<<iter2->first<<' '<<iter2->second<<endl;
return 0;
}
8、查找函数count()
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
ma[1] = "student_1";
ma[2] = "student_2";
ma[3] = "student_3";
map<int, string>::iterator iter1;
map<int, string>::reverse_iterator iter2;
if ( ma.count(1) )
printf("1 apper\n");
else
printf("1 not apper\n");
if ( ma.count(4) )
printf("4 apper\n");
else
printf("4 not apper\n");
return 0;
}
9、查找函数find()
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
ma[1] = "student_1";
ma[2] = "student_2";
ma[3] = "student_3";
map<int, string>::iterator iter1;
map<int, string>::reverse_iterator iter2;
if ( ma.find(1) != ma.end() ){
printf("1 apper\n");
cout<<ma.find(1)->first<<" ";
cout<<ma.find(1)->second<<endl;
}
else
printf("1 not apper\n");
if ( ma.find(4) != ma.end() )
printf("4 apper\n");
else
printf("4 not apper\n");
return 0;
}
10、lower_bound()和upper_bound()的用法
lower_bound函数用法,用来返回要查找关键字的下界(是一个迭代器)
upper_bound函数用法,用来返回要查找关键字的上界(是一个迭代器)
Emple:
map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper_bound(2)的话,返回的就是3
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> ma;
ma[1] = "student_1";
ma[3] = "student_3";
ma[5] = "student_5";
map<int, string>::iterator iter;
iter = ma.lower_bound(1);
//返回的是下界1的迭代器
cout<<iter->second<<endl;
iter = ma.lower_bound(2);
//返回的是下界3的迭代器
cout<<iter->second<<endl;
iter = ma.lower_bound(3);
//返回的是下界3的迭代器
cout<<iter->second<<endl;
iter = ma.upper_bound(2);
//返回的是上界3的迭代器
cout<<iter->second<<endl;
iter = ma.upper_bound(3);
//返回的是上界5的迭代器
cout<<iter->second<<endl;
return 0;
}
11、map基本函数
map的基本操作函数:
C++ maps是一种关联式容器,包含“关键字/值”对
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的函数