map/multimap基本概念
Map的特性是,所有元素都会根据元素的键值自动排序。Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。
我们可以通过map的迭代器改变map的键值吗?答案是不行,因为map的键值关系到map元素的排列规则,任意改变map键值将会严重破坏map组织。如果想要修改元素的实值,那么是可以的。
Map和list拥有相同的某些性质,当对它的容器元素进行新增操作或者删除操作时,操作之前的所有迭代器,在操作完成之后依然有效,当然被删除的那个元素的迭代器必然是个例外。
Multimap和map的操作类似,唯一区别multimap键值可重复。
Map和multimap都是以红黑树为底层实现机制。
常用方法
map构造函数
map<T1, T2> mapTT;//map默认构造函数:
map(const map &mp);//拷贝构造函数
map赋值操作
map& operator=(const map &mp);//重载等号操作符
swap(mp);//交换两个集合容器
map大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
map插入数据元素操作
map.insert(...); //往容器插入元素,返回pair<iterator,bool>
map<int, string> mapStu;
// 第一种 通过pair的方式插入对象
mapStu.insert(pair<int, string>(3, "Sam"));
// 第二种 通过pair的方式插入对象
mapStu.inset(make_pair(-1, "Jack"));
// 第三种 通过value_type的方式插入对象
mapStu.insert(map<int, string>::value_type(1, "John"));
// 第四种 通过数组的方式插入值
mapStu[4] = "Tom";
clear();//删除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(keyElem);//删除容器中key为keyElem的对组。
示例
#include<iostream>
#include<map>
#include<string>
using namespace std;
void printMap(map<int, int> m) {
for (map<int, int>::iterator i = m.begin(); i != m.end(); ++i) {
cout <<"key=" << (*i).first <<" value=" << (*i).second << endl;
}
}
void test01() {
map<int,int> m1;
m1.insert(pair<int,int>(1,10));
m1.insert(make_pair(2,20));
m1.insert(map<int, int>::value_type(3,30));
m1.insert(pair<int,int>(4,40));
m1.insert(make_pair(5,50));
m1.insert(map<int,int>::value_type(6,60));
m1[7]= 70;
m1.erase(4);
printMap(m1);
map<int, int>::iterator ret = m1.find(6);
cout << "key" << ret->first << " value:" << ret->second << endl;
int num = m1.count(5);
cout << "key为5的对组个数为: " << num << endl;
}
void test02() {
map<int, int> m1;
m1.insert(make_pair(1,10));
m1.insert(pair<int, int>(2, 20));
m1.insert(map<int,int>::value_type(3,30));
m1.insert(make_pair(4, 40));
m1.insert(pair<int, int>(5,50));
m1.insert(map<int, int>::value_type(6, 60));
m1[7] = 70;
map<int, int>::iterator pos = m1.lower_bound(3);
cout <<"key:" << pos->first << " value:" << pos->second << endl;
pos = m1.upper_bound(3);
cout << "key:" << pos->first << " value:" << pos->second << endl;
pair<map<int, int>::iterator, map<int, int>::iterator> ret = m1.equal_range(3);
cout << "key" << ret.first->first << " value:" << ret.first->second << endl;
cout << "key" << ret.second->first << " value:" << ret.second->second << endl;
}
class comparePerson;
class person {
friend class comparePerson;
friend void test03();
private:
string name;
int age;
int salary;
public:
person(string name,int age,int salary) {
this->name = name;
this->age = age;
this->salary = salary;
}
~person() {}
};
class myCompare
{
public:
bool operator()(int v1,int v2)const
{
return v2 < v1;
}
};
void test03() {
map<int,person, myCompare> m1;
person p1("Tom", 21, 4256);
person p2("Sam", 25, 6256);
person p3("John", 23, 6788);
person p4("Lucy", 33, 4678);
person p5("Jack", 47, 3154);
person p6("Tim", 36, 6788);
person p7("Jim", 56, 6256);
m1.insert(make_pair(1, p1));
m1.insert(pair<int,person>(2, p2));
m1.insert(map<int,person>::value_type(3,p3));
m1.insert(make_pair(4, p4));
m1.insert(pair<int, person>(5, p5));
m1.insert(map<int,person>::value_type(6,p6));
for (map<int, person, myCompare>::iterator i = m1.begin(); i != m1.end(); ++i) {
cout << "key:" << i->first << " name:" << i->second.name << " age:" << i->second.age << " salary:" << i->second.salary << endl;
}
}
int main() {
test01();
cout << "-------------------------" << endl;
test02();
cout << "-------------------------" << endl;
test03();
system("pause");
return EXIT_SUCCESS;
}
执行结果
key=1 value=10
key=2 value=20
key=3 value=30
key=5 value=50
key=6 value=60
key=7 value=70
key6 value:60
key为5的对组个数为: 1
-------------------------
key:3 value:30
key:4 value:40
key3 value:30
key4 value:40
-------------------------
key:6 name:Tim age:36 salary:6788
key:5 name:Jack age:47 salary:3154
key:4 name:Lucy age:33 salary:4678
key:3 name:John age:23 salary:6788
key:2 name:Sam age:25 salary:6256
key:1 name:Tom age:21 salary:4256
请按任意键继续. . .