map是STL的一个关联容器,它提供一对一(Key / Value)的hash。其特性如下:
map的所有元素都是pair,同时拥有键值(key)和实值(value)。pair的第一个元素会被视为键值,第二个元素会被视为实值。
map不允许两个元素拥有相同的键值。
对于迭代器来说,可以修改Value值,而不能修改Key值。
map会根据key自动排序。
一般不使用下标访问元素
使用STL标准库的 map 时,应包含头文件:#include
1、定义
map<string, int> strMap;
map<string, char> strMap;
map<char, string> charMap;
map<char, int> charMap;
map<int, char> intMap;
map<int, string> intMap;
map<k, y> m;
//创建空map,k和v可以是任意类型
map<int, string> m2(m1);
//创建m1的副本m2,m1与m2必须有相同的键类型和值类型
map<int, string> m2(m1.begin(), m1.end());
//创建map类型的对象m2,存储迭代器b和e标记的范围内所有元素的副本
2、基本操作
m.begin() 返回指向map头部的迭代器
m.end() 返回指向map末尾的迭代器
m.clear() 删除所有元素
m.count() 返回指定元素出现的次数
m.empty() 如果map为空则返回true
m.equal_range() 返回特殊条目的迭代器对
m.erase() 删除一个元素
m.find() 查找一个元素
m.get_allocator() 返回map的配置器
m.insert() 插入元素
m.key_comp() 返回比较元素key的函数
m.lower_bound() 返回键值>=给定元素的第一个位置
m.max_size() 返回可以容纳的最大元素个数
m.rbegin() 返回一个指向map尾部的逆向迭代器
m.rend() 返回一个指向map头部的逆向迭代器
m.size() 返回map中元素的个数
m.upper_bound() 返回键值>给定元素的第一个位置
m.value_comp() 返回比较元素value的函数
m1.swap(m2) 交换两个map
swap(m1, m2) 交换两个map
1、基本用法
#include
#include
using namespace std;
int main(){
map<char,int> m;
m['b'] = 100;
m['a'] = 200;
m['c'] = 300;
map<char,int>::iterator it=m.begin();
for(it; it!=m.end(); ++it)
cout<<it->first<<" "<<it->second<<endl;
cout<<"大小:"<<m.size()<<endl;
while(!m.empty()){
cout<<m.begin()->first<<" "<<m.begin()->second<<endl;
m.erase(m.begin());
}
return 0;
}
2、map中元素的插入
#include
#include
using namespace std;
int main(){
map<int, int> m;
for (int i=0; i<10; i++)
m[i]=i;
for (int i=10; i<20; i++)
m.insert(pair<int, int>(i, i));
map<int, int> m1;
m1[20]=20;
m1[21]=21;
m.insert(m1.begin(), m1.end());
map<int, int>::iterator it;
for (it=m.begin(); it!=m.end(); it++)
cout<<it->first<<" "<<it->second<<endl;
return 0;
}
输出结果:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
3、元素的查找和读取
#include
#include
using namespace std;
int main(){
map<int, int> m;
for(int i=0; i<20; i++)
m.insert(make_pair(i, i));
if(m.count(0))
cout<<"yes!\n";
else
cout<<"no!\n";
map<int, int>::iterator it1;
it1 = m.find(0);
if(it1 != m.end())
it1 -> second = 20;
else
cout<<"no!\n";
map<int, int>::iterator it2;
for (it2 = m.begin(); it2 != m.end(); it2++)
cout<<it2->first<<"->"<<it2->second<<endl;
return 0;
}
4、map中元素的删除
#include
#include
using namespace std;
int main(){
map<int, int> m;
for(int i=0; i<20; i++)
m.insert(make_pair(i, i));
m.erase(0);
m.erase(m.begin());
map<int, int>::iterator it;
for(it=m.begin(); it!=m.end(); it++)
cout<<it->first<<" -> "<<it->second<<endl;
return 0;
}
5、交换两个map所有元素
#include
#include
using namespace std;
int main(){
map<int, string> m1;
map<int, string>::iterator it1 = m1.begin();
m1[1] = "one";
m1[2] = "two";
m1[3] = "three";
m1[4] = "four";
m1[5] = "five";
map<int, string> m2;
map<int, string>::iterator it2 = m2.begin();
m2[1] = "One";
m2[2] = "Two";
m2[3] = "Three";
m1.swap(m2);
it1 = m1.begin();
for(it1; it1!= m1.end(); it1++)
cout<<(*it1).first<<" "<<(*it1).second<<"\n";
cout<<endl;
it2 = m2.begin();
for(it2; it2!=m2.end(); it2++)
cout<<(*it2).first<<" "<<(*it2).second<<"\n";
return 0;
}