前言:map是C++STL中的一个关联容器,它提供一对一的哈希值。map中的每一个元素都由2个相同或者不同的数据类型组成
第一个(first)数据类型可以称为关键字(key),每个关键字只能在map中出现一次
第二个(second)数据类型可以称为该关键字的值(value)
1.map的定义
map<int, string>map1,map2;
2.map的插入
map1.insert(pair<int, string>(100, "张三"));
map1[101] = "李四";//常用
/*重复插入时,后写的语句不会生效*/
3.map的遍历
/*使用迭代器来对map来进行遍历*/
map<int, string>::iterator i1;
for (i1 = map1.begin(); i1 != map1.end(); i1++)
cout << i1->second << ' ';
cout << endl;
cout << endl;
/*反向遍历*/
map<int, string>::reverse_iterator i3;
for (i3 = map1.rbegin(); i3 != map1.rend(); i3++)
cout << i1->second << ' ';
cout << endl;
cout << endl;
/*数组遍历*/
int n = map1.size();
for (int i = 1; i <= n; i++)//i不能从0开始
{
cout << map1[i] << ' ';
}
cout << endl;
cout << endl;
4.map的查找
map<int, string>::iterator i2;
i2 = map1.find(100);
/*如果100对应的值不存在,i2=map1.end()*/
cout << i2->second << endl;
cout << endl;
/*upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
例如:map中已经插入了1,2,3,4的话,
如果lower_bound(2)的话,返回的2,
而upper-bound(2)的话,返回的就是3*/
5.map的删除
map1.erase(100);
/*map1.erase(iterator first,iterator last),删除一个范围*/
/*map1.clear(),清空*/
for (i1 = map1.begin(); i1 != map1.end(); i1++)
cout << i1->second << ' ';
cout << endl;
cout << endl;
//map的交换
map2[1] = "a";
map2[2] = "b";
map2[3] = "c";
map2[4] = "d";
map2[5] = "e";
map1.swap(map2);
for (i1 = map1.begin(); i1 != map1.end(); i1++)
cout << i1->second << ' ';
cout << endl;
for (i2 = map2.begin(); i2 != map2.end(); i2++)
cout << i2->second << ' ';
cout << endl;
6.map的基本操作函数表
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的函数
7.map完整测试代码
#pragma GCC optimize(2)
#include<iostream>
#include<iomanip>
#include<utility>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include<ctime>
#include<cstring>
#include<list>
#define ll long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> PII;
void solve()
{
//map的定义
/*一个map包含2个数据类型,第一个称为关键字,
每个关键字在map中只能出现一次,
第二个则称为该关键字的值*/
map<int, string>map1,map2;
//map的插入
map1.insert(pair<int, string>(100, "张三"));
map1[101] = "李四";//常用
/*重复插入时,后写的语句不会生效*/
//map的遍历
/*使用迭代器来对map来进行遍历*/
map<int, string>::iterator i1;
for (i1 = map1.begin(); i1 != map1.end(); i1++)
cout << i1->second << ' ';
cout << endl;
cout << endl;
/*反向遍历*/
map<int, string>::reverse_iterator i3;
for (i3 = map1.rbegin(); i3 != map1.rend(); i3++)
cout << i1->second << ' ';
cout << endl;
cout << endl;
/*数组遍历*/
int n = map1.size();
for (int i = 1; i <= n; i++)//i不能从0开始
{
cout << map1[i] << ' ';
}
cout << endl;
cout << endl;
//map的查找
map<int, string>::iterator i2;
i2 = map1.find(100);
/*如果100对应的值不存在,i2=map1.end()*/
cout << i2->second << endl;
cout << endl;
/*upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
例如:map中已经插入了1,2,3,4的话,
如果lower_bound(2)的话,返回的2,
而upper-bound(2)的话,返回的就是3*/
//map的删除
map1.erase(100);
/*map1.erase(iterator first,iterator last),删除一个范围*/
/*map1.clear(),清空*/
for (i1 = map1.begin(); i1 != map1.end(); i1++)
cout << i1->second << ' ';
cout << endl;
cout << endl;
//map的交换
map2[1] = "a";
map2[2] = "b";
map2[3] = "c";
map2[4] = "d";
map2[5] = "e";
map1.swap(map2);
for (i1 = map1.begin(); i1 != map1.end(); i1++)
cout << i1->second << ' ';
cout << endl;
for (i2 = map2.begin(); i2 != map2.end(); i2++)
cout << i2->second << ' ';
cout << endl;
//map的基本操作函数表
/*
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的函数
*/
}
int main()
{
solve();
return 0;
}
作者:Avalon·Demerzel