1.
map中所有元素都是pai对组,pair中的第一个元素是key(键值),起到索引作用,第二个元素为value(实际值),并且map中的所有元素都会根据元素的键值自动排序,map/multimap本质上是一个关联式容器,底层结构是用二叉树实现的。
map/multimap具有使用key值快速查找到value值的有点,map与multimap的区别是前者不允许容器中有重复的key值元素,后者则恰恰相反它允许容器中有重复的key值元素。
- map构造和赋值
构造函数:
map <T1,T2>mp; //默认构造函数
map(const map &mp); //拷贝构造函数
其中T1代表key,T2代表value
赋值:
map& operator=(const map &mp);
该容器重载了=号运算符支持赋值操作
- 插入数据的操作:
mp.insert(pair<T1,T2>(key,value));
mp.insert(make_pair(key,value));
mp.insert(map<T1,T2>::value_type(key,value))
mp[key] = value;
因为map/multimap中的元素是以对组pair出现的,所以插入的数据必须是pair对组;上面的插入插座也可以将pari<T1,T2>换成make_pair,也可以是map容器自带的方法value_type,还可以使用中括号,中括号里面是索引(key)右边是value,这里顺带提一下中括号,在使用自定义类型的时候(比如类)需要保留默认构造,具体如下:
class Person
{
public:
Person(){} //不加这里的话[]会报错
Person(string nam,int ag)
{
this->name = nam;
this->age = ag;
}
string name;
int age;
};
...
Person p1("张三",22);
map <int,Person> mp;
mp[1] = p2;
我之前就是忽略了这个默认构造所以使用[]的时候就报错,但是不建议使用这种方法来插入数据,有时候在使用中括号是运算符的时候如果没有它会自动创建出一个value,所以只用它来访问已经存在的元素,比如:
map <int,int> mp;
mp.insert(make_pair(0,10));
cout << mp[0] << endl; //这里打印就是10
cout << mp[1] << endl; //如果5对应的value没有那么它就会自动创建一个
- 删除操作
mp.erase(key);
map<T1,T2>::iterator it = mp.begin();
mp.erase(++it,mp.end());
mp.clear();
第一种是传入以个key值,删除key值的对应对组元素,第二种是使用迭代器范围的,但是这里不支持随it+n操作(it指map的迭代器);第三种是清空整个map容器。
- 同样它也支持自定义排序,这里需要用到仿函数,示例如下:
class Person
{
public:
Person(){}
Person(string nam,int ag)
{
this->name = nam;
this->age = ag;
}
string name;
int age;
};
class Mcompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void print(map<int,Person,Mcompare> &p)
{
for(map<int,Person,Mcompare>::iterator it = p.begin();it != p.end();it++)
{
cout << " 编号:" <<it->first << " 姓名: " << (it->second).name << " 年龄: " <<(it->second).age << endl;
}
cout << endl;
}
...
Person p1("张三",22);
Person p2("李四",28);
Person p3("王五",21);
map <int,Person,Mcompare> mp5;
mp5.insert(make_pair(0,p1));
mp5.insert(make_pair(9,p2));
mp5.insert(make_pair(3,p3));
- 查找和统计:
find(key);
如果存在返回该键值所对应元素的迭代器,如果不存在返回end迭代器;
例如:
if(mp.find(1) != mp.end())
{
cout << "have find key 1"<< "the value is"<< ((mp.find(1))->second).name << " "<< ((mp.find(1))->second).age<<endl;
}
map <int,Person>::iterator it = mp.end();
if(mp.find(4) == it)
{
cout << "can't have find key 4"<<endl;
}
统计:
count(key)
统计对应key的元素个数,如果是map容器则返回0或者1(因为map不允许key值相同的元素),如果是multimap容器则会返回n
常用到的方法:
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
equal_range() 返回特殊条目的迭代器对
get_allocator() 返回map的配置器
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数
示例代码
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
class Person
{
public:
Person(){}
Person(string nam,int ag)
{
this->name = nam;
this->age = ag;
}
string name;
int age;
};
class Mcompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void print(map<int,Person> &p)
{
for(map<int,Person>::iterator it = p.begin();it != p.end();it++)
{
cout << " 编号:" <<it->first << " 姓名: " << (it->second).name << " 年龄: " <<(it->second).age << endl;
}
cout<< endl;
}
void Mprint(map<int,Person,Mcompare> &p)//对print进行重载
{
for(map<int,Person,Mcompare>::iterator it = p.begin();it != p.end();it++)
{
cout << " 编号:" <<it->first << " 姓名: " << (it->second).name << " 年龄: " <<(it->second).age << endl;
}
cout << endl;
}
void test01(void)
{
Person p1("张三",22);
Person p2("李四",28);
Person p3("王五",21);
map <int,Person> mp;
if(mp.empty())
{
cout << "mp is empty" << endl;
}
mp.insert(pair<int,Person>(1,p1));
mp.insert(pair<int,Person>(0,p2));
mp.insert(pair<int,Person>(2,p3));
cout << "mp: "<< endl;
print(mp);
if(!mp.empty())
{
cout << "mp is not empty" << endl;
}
cout <<"mp1: "<< endl;
map<int,Person> mp1(mp);
print (mp1);
cout << "mp2: "<<endl;
map<int,Person> mp2= mp;
print(mp2);
cout << "mp2 size:" << mp2.size() << endl;
Person p4("赵六",19);
Person p5("老八",27);
Person p6("漆九",21);
map<int, Person> mp3;
mp3.insert(pair<int ,Person>(8,p4));
mp3.insert(make_pair(7,p5));
mp3.insert(map<int,Person>::value_type(10,p6));
cout <<"mp3 before swap: " <<endl;
print(mp3);
cout <<"mp3 after swap(mp2): " <<endl;
mp3.swap(mp2);
print(mp3);
cout << "mp2 after swap: " <<endl;
print(mp2);
map <int,Person,Mcompare> mp5;
mp5.insert(make_pair(0,p1));
mp5.insert(pair<int,Person>(9,p2));
mp5.insert(make_pair(3,p3));
mp5.insert(make_pair(1,p4));
cout << "mp5: (使用仿函数)"<< endl;
Mprint(mp5);
// cout << "earse 9: " << endl;
// mp5.erase(9);
cout << "对应的范围的删除" << endl;
map<int,Person,Mcompare>::iterator it = mp5.begin();
map<int,Person,Mcompare>::iterator it1 = mp5.end();
mp5.erase(++it,--it1);
Mprint(mp5);
cout << "清空 mp3:" <<endl;
mp3.clear();
if(mp3.empty())
{
cout << "mp3 is empty" << endl;
}
}
void print(const map<int,int> &p)
{
for(map<int,int>::const_iterator it = p.begin();it != p.end();it++)
{
cout << it->first << " " << it->second << endl;
}
cout << endl;
}
void test02(void)
{
cout << "------------------------------------------"<<endl;
map<int,int> mp;
mp[0] = 1;
print(mp);
}
void test03(void)
{
Person p1("张三",22);
Person p2("李四",28);
Person p3("王五",21);
map <int,Person> mp;
mp.insert(make_pair(0,p1));
mp.insert(make_pair(0,p1));
mp.insert(make_pair(1,p2));
mp.insert(make_pair(2,p3));
mp[1] = p2;
print(mp);
cout << "mp key 0 numbers: " << mp.count(0) << endl;
cout << "find 0" << endl;
if(mp.find(1) != mp.end())
{
cout << "have find key 1"<< "the value is"<< ((mp.find(1))->second).name << " "<< ((mp.find(1))->second).age<<endl;
}
map <int,Person>::iterator it = mp.end();
if(mp.find(4) == it)
{
cout << "can't have find key 4"<<endl;
}
multimap<int,Person> mp2;
mp2.insert(make_pair(0,p1));
mp2.insert(make_pair(0,p1));
mp2.insert(make_pair(0,p1));
cout << "mp2 key 0 numbers: " << mp2.count(0) << endl;
}
int main(void)
{
test01();
test02();
test03();
return 0;
}