一.特性
- map相对于set区别,map具有键值和实值,所有元素根据键值自动排序。pair的第一个元素被称为键值,第二个元素被称为实值。map也是红黑树为底层实现机制。
- map根据key排序。
- map中key不能重复,multimap中key可以重复。
- 不能通过map迭代器修改map的键值,因为容器安装key排序,修改后规则发生变化。
- 可以改变map的实值。
- 如果想修改map的键值,应该先删除该结点,然后插入新结点。
二.构造函数
map<int, string> m1,m2;//默认构造函数
map(const map & m1);//拷贝构造函数
三.赋值操作
map<int, string> m1, m2;//默认构造函数
map& operator=(const map & m1);//重载等号操作符
m2.swap(m1);//交换两个集合容器
四.大小操作
map<int, string> m1, m2;//默认构造函数
m1.size();//返回容器中元素数目
m1.empty();//判断容器是否为空
五.插入元素
map<int, string> m1;
//第一种方式:通过pair对方式插入对象
m1.insert(pair<int, string>(3, "隔壁"));
//第二种方式:通过pair对象插入
m1.insert(make_pair(4,"老王"));
//第三种方式:通过value_type方式插入
m1.insert(map<int, string>::value_type(5, "理智"));
//第四种方式:通过数组方式插入
m1[1] = "a";
m1[40] = "b";
for (map<int, string>::iterator it = m1.begin(); it != m1.end();it++) {
//it取出一对pair
cout << "key="<<(*it).first <<",value="<< (*it).second << endl;
}
结果:
六.插入问题
1.测试方法一、二、三、四
map<int, int> m2;
//插入方式一、二、三
pair<map<int, int>::iterator, bool> ret = m2.insert(pair<int, int>(10, 10));
if (ret.second != false) {
cout << "第一次插入成功!" << endl;
}
else {
cout << "第二次插入失败!" << endl;
}
ret = m2.insert(pair<int, int>(10, 20));
if (ret.second != false) {
cout << "第二次插入成功!" << endl;
}
else {
cout << "第二次插入失败!" << endl;
}
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
cout << "first=" << (*it).first << ",second" << (*it).second << endl;
}
//插入方式四
m2[10] = 20;
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
cout << "first=" << (*it).first << ",second" << (*it).second << endl;
}
结果:
用方法一、二、三插入key相同,value不同的元素,不能插入成功。
用方法四插入key相同,value不同的元素可以插入成功。其实质是修改对应的value。
2.用方法四访问一个不存在的数
m2[10] = 20;
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
cout << "first=" << (*it).first << ",second=" << (*it).second << endl;
}
cout << "------------------" << endl;
cout << m2[20] << endl;
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
cout << "first=" << (*it).first << ",second=" << (*it).second << endl;
}
结果:用方法四访问一个不存在的数,那么map会自动插入这个不存在的数,value默认为0;
七.查找操作
map<int,int> st;
st.find(key);//查找key是否存在,若存在,返回该键的元素迭代器,若不存在,返回map.end()
st.lower_bound(keyelem);//返回第一个key>=keyelem元素的迭代器
st.upper_bound(keyelem);//返回第一个key>keyelem元素的迭代器
st.equal_range(keyelem);//返回容器key与keyelem相等的上下限的两个迭代器
1.测试find
map<int, int> m1;
m1.insert(pair<int, int>(1, 2));
m1.insert(pair<int, int>(3, 4));
m1.insert(pair<int, int>(5, 6));
map<int, int>::iterator it = m1.find(6);
if (it == m1.end()) {
cout << "1" << endl;
}
结果:
map<int, int> m1;
m1.insert(pair<int, int>(1, 2));
m1.insert(pair<int, int>(3, 4));
m1.insert(pair<int, int>(5, 6));
map<int, int>::iterator it = m1.find(3);
cout << it->first << endl;
cout << it->second << endl;
2.测试equal_range
map<int, int> m;
m.insert(make_pair(1, 4));
m.insert(make_pair(2, 5));
m.insert(make_pair(3, 6));
pair<map<int, int>::iterator, map<int, int>::iterator> it = m.equal_range(2);
if (it.first == m.end()) {
cout << "没找到!" << endl;
}
else {
cout << (it.first)->first << "," << (it.first)->second << endl;
}
if (it.second == m.end()) {
cout << "没找到!" << endl;
}
else {
cout << (it.second)->first << "," << (it.second)->second << endl;
}
结果:
八.自定义类型排序
先看程序:
class MyKey {
public:
MyKey(int index, int id) {
this->mindex = index;
this->mid = id;
}
int mindex;
int mid;
};
int main() {
map<MyKey, int> mymap;
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(4, 5), 20));
}
然后进行编译:
出错。
原因:map执行insert需要排序key,而自定义类型,map没有默认排序规则。
自己编写排序规则(仿函数):
class MyKey {
public:
MyKey(int index, int id) :mindex(index),mid(id) {}
int mindex;
int mid;
};
struct mycompare {//仿函数
bool operator()(MyKey mk1, MyKey mk2) const{
return mk1.mindex > mk2.mindex;
}
};
int main() {
map<MyKey, int, mycompare> mymap;
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(6, 7), 30));
mymap.insert(make_pair(MyKey(4, 5), 20));
for (map<MyKey, int, mycompare>::iterator it = mymap.begin(); it != mymap.end(); it++) {
cout << "first:" << "mindex=" << (it->first).mindex << ",mid=" << (it->first).mid << ",second:" << (it->second) << endl;
}
}
结果: