成对出现的数据,利用对组可以返回两个数据;
pair中第一个元素为key(键值),起到索引的作用,第二个元素是value值(实际值)
pair
pair
#include #include map中所有的元素都是对组pair
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
所有的元素都会根据key值来进行自动排序
map存出结构和set一样的都是基于红黑树的,所以map不能修改key值,但是可以修改value值
可以使用map[key]来获取key值对应的value值,at(key)也能获取
不支持随机存取,因为不是连续的物理空间,所以迭代器不能进行算术运算,只能进行++或者--
map/multimap属于关联式容器,底层结构是用二叉树实现。
可以根据key值快速找到value值
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素
功能描述:对map容器进行构造和赋值操作
map
map(const map&mp) // 拷贝构造
map
map& operator=(const map&mp); //重载等号操作符
可以用map[key]的方式获取key对应的value值, at(key)也可以
// 声明一个通用的打印函数 需要使用C++11新特性实现 template void printMap(map& m) { for (auto p : m) { cout << p.first << ":" << p.second << endl; } } void test02() { // 创建一个新的map容器 map m; m.insert(pair(1, 10)); m.insert(pair(2, 20)); m.insert(pair(3, 30)); m.insert(pair(4, 40)); //printMap(m); // 拷贝构造 map m1(m); //printMap(m1); //赋值 map m2; m2 = m1; //printMap(m2); // 插入元素 m.insert(make_pair(5, 50)); m[6] = 60; //printMap(m); // 取值 cout << "key = 3:" << m[3] << endl; cout << "key = 3:" << m.at(3) << endl; m[3] = 300; //printMap(m); // 删除元素 m.erase(m.begin()); printMap(m); m.erase(m.begin(), ++m.begin()); printMap(m); m.erase(6); printMap(m); } map中所有元素都是成对出现,插入数据时候要使用对组。
功能描述: map容器进行插入数据和删除数据
insert(elem); // 在容器中插入元素,返回插入元素的迭代器
map[key] // key存在时是修改value,如果不存在是添加key和value(不建议)
clear() // 清空容器
erase(pos) // 删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end) // 删除区间[beg,end)中的元素,返回下一个元素的迭代器
erase(key); // 删除容器中值为key的元素
void printMap(map&m) { for (map::iterator it = m.begin(); it != m.end(); it++) { cout << "key = " << it->first << " value = " << it->second << endl; } cout << endl; } void test01() { //插入 map m; //第一种插入方式 m.insert(pair(1, 10)); //第二种插入方式 m.insert(make_pair(2, 20)); //第三种插入方式 m.insert(map::value_type(3, 30)); //第四种插入方式 m[4] = 40; printMap(m); //删除 m.erase(m.begin()); printMap(m); m.erase(3); printMap(m); //清空 m.erase(m.begin(),m.end()); m.clear(); printMap(m); } map插入方式很多,记住其一即可
插入 --- insert
删除 --- erase
清空 --- clear
map和multimap只有在删除当前的iterator的时候才会失效,只要在erase时,更新迭代器即可,插入和删除不会对其他节点造成影响
插入操作不会导致任何迭代器失效
// 迭代器失效 // 只有删除会导致迭代器失效,需要更新,插入数据的时候并不会引起迭代器失效 void test03() { map m; m.insert(pair(1, 10)); m.insert(pair(2, 20)); m.insert(pair(3, 30)); for (map::iterator it = m.begin(); it != m.end(); it++) { if ((*it).first == 2) { it = m.erase(it); } } printMap(m); } 功能描述: 统计map容器大小以及交换map容器
size() // 返回容器中元素的个数
empty() // 判断容器是否为空
swap(st) // 交换两个集合容器
void printMap(map&m) { for (map::iterator it = m.begin(); it != m.end(); it++) { cout << "key = " << it->first << " value = " << it->second << endl; } cout << endl; } void test01() { mapm; m.insert(pair(1, 10)); m.insert(pair(2, 20)); m.insert(pair(3, 30)); if (m.empty()) { cout << "m为空" << endl; } else { cout << "m不为空" << endl; cout << "m的大小为: " << m.size() << endl; } } //交换 void test02() { mapm; m.insert(pair(1, 10)); m.insert(pair(2, 20)); m.insert(pair(3, 30)); mapm2; m2.insert(pair(4, 100)); m2.insert(pair(5, 200)); m2.insert(pair(6, 300)); cout << "交换前" << endl; printMap(m); printMap(m2); cout << "交换后" << endl; m.swap(m2); printMap(m); printMap(m2); } 统计大小 --- size
判断是否为空 --- empty
交换容器 --- swap
功能描述: 对map容器进行查找数据以及统计数据
find(key); // 查找key是否存在,如果存在就返回该键的元素的迭代器,如果不存在就返回map.end();
count(key); // 查找容器中有多少个key元素个数,对于map而言只能是0或者是1
//查找和统计 void test01() { mapm; m.insert(pair(1, 10)); m.insert(pair(2, 20)); m.insert(pair(3, 30)); //查找 map::iterator pos = m.find(3); if (pos != m.end()) { cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl; } else { cout << "未找到元素" << endl; } //统计 int num = m.count(3); cout << "num = " << num << endl; } 查找 --- find (返回的是迭代器)
统计 --- count (对于map,结果为0或者1)
功能描述:map容器默认排序规则为 按照key值进行 从小到大排序
利用仿函数可以指定map容器的排序规则
对于自定义类型,map必须指定排序规则,同set容器
// 利用仿函数改变默认排序规则 class myCompare { public: // 在类中重载()运算符 bool operator()(int v1, int v2) const // 和set规则一致 { return v1 > v2; } }; void test06() { map m; m.insert(pair(1, 10)); m.insert(pair(2, 20)); m.insert(pair(3, 30)); for (auto p : m) { cout << p.first << ":" << p.second << endl; } } // map容器根据value值来进行排序 // 根据value值来排序,通过vector容器,将map容器转化为vector,然后利用排序算法sort来实现 bool paiSort(pair &p1, pair &p2) { return p1.second > p2.second; // 根据年龄进行排序 } void test07() { map mp; mp.insert(make_pair("tom", 18)); mp.insert(make_pair("jerry", 8)); mp.insert(make_pair("dijia", 12)); printMap(mp); // 使用vector来对value值进行排序,需要将map中所有的对组放到vector中 vector> v(mp.begin(), mp.end()); // 利用算法sort来对vector中的元素排序 sort(v.begin(), v.end(), paiSort); for (auto p : v) { cout << p.first << ":" << p.second << endl; } } 利用仿函数可以指定map容器的排序规则
对于自定义数据类型,map必须要指定排序规则,同set容器
// multimap中key值可以重复,那么如果直接用[]来修改value值会有问题 void test05() { multimap mp; mp.insert(make_pair("tom", 20)); mp.insert(make_pair("tom", 10)); mp.insert(make_pair("lucy", 18)); mp.insert(make_pair("jerry", 25)); mp.insert(make_pair("jerry", 8)); mp.insert(make_pair("tom", 40)); for (auto ele : mp) { cout << ele.first << ":" << ele.second << endl; } // 既然key有可能出现重复的情况,那么就可以通过find()和count()来获取有多少相同的key,然后再去选择修改哪一个key int mCount = mp.count("tom"); // 先查找有几个tom multimap::iterator pos = mp.find("tom"); // 找到第一个tom for (int i = 0; i < mCount; i++) { cout << pos->first << ":" << pos->second << endl; pos++; } } // 修改multimap中相同的key键对应的某个value值 void test03() { multimap mp; mp.insert(make_pair("tom", 18)); mp.insert(make_pair("dijia", 18)); mp.insert(make_pair("tom", 28)); mp.insert(make_pair("tom", 38)); // equal_range() 返回pair对象,其中first和second成员会全部转化为迭代器 // 所以可以进行遍历,遍历时通过second的值来进行判断, 符合条件的直接修改当前迭代器对应的second值 auto range = mp.equal_range("tom"); for (auto it = range.first; it != range.second; it++) { // 找到要修改的tom对应的28.,改为100即可 if (it->second == 28) { it->second = 100; } } for (auto it = range.first; it != range.second; it++) { cout << it->first << " : " << it->second << endl; } } #include #include #include