第 18.10 節
map / multimap 容器
0瀏覽次數0訪問次數--跳出率--平均停留
map/ multimap容器
map基本概念
簡介:
- map中所有元素都是pair
- pair中第一個元素為key(鍵值),起到索引作用,第二個元素為value(實值)
- 所有元素都會根據元素的鍵值自動排序
本質:
- map/multimap屬於關聯式容器,底層結構是用二叉樹實現。
優點:
- 可以根據key值快速找到value值
map和multimap區別:
- map不允許容器中有重複key值元素
- multimap允許容器中有重複key值元素
map構造和賦值
功能描述:
- 對map容器進行構造和賦值操作
函數原型:
構造:
map<T1, T2> mp;//map默認構造函數:map(const map &mp);//拷貝構造函數
賦值:
map& operator=(const map &mp);//重載等號操作符
示例:
#include <iostream>
#include <map>
using namespace std;
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int,int>m; //默认构造
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
map<int, int>m2(m); //拷贝构造
printMap(m2);
map<int, int>m3;
m3 = m2; //赋值
printMap(m3);
}
int main() {
test01();
return 0;
}
運行結果:
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
總結:map中所有元素都是成對出現,插入數據時候要使用對組
map大小和交換
功能描述:
- 統計map容器大小以及交換map容器
函數原型:
size();//返回容器中元素的數目empty();//判斷容器是否為空swap(st);//交換兩個集合容器
示例:
#include <iostream>
#include <map>
using namespace std;
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大小为: " << m.size() << endl;
}
}
//交换
void test02()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前" << endl;
printMap(m);
printMap(m2);
cout << "交换后" << endl;
m.swap(m2);
printMap(m);
printMap(m2);
}
int main() {
test01();
test02();
return 0;
}
運行結果:
m不为空
m的大小为: 3
交换前
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 100
key = 5 value = 200
key = 6 value = 300
交换后
key = 4 value = 100
key = 5 value = 200
key = 6 value = 300
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
總結:
- 統計大小 --- size
- 判斷是否為空 --- empty
- 交換容器 --- swap
map插入和刪除
功能描述:
- map容器進行插入數據和刪除數據
函數原型:
insert(elem);//在容器中插入元素。clear();//清除所有元素erase(pos);//刪除pos迭代器所指的元素,返回下一個元素的迭代器。erase(beg, end);//刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。erase(key);//刪除容器中值為key的元素。
示例:
#include <iostream>
#include <map>
using namespace std;
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
//插入
map<int, int> m;
//第一种插入方式
m.insert(pair<int, int>(1, 10));
//第二种插入方式(常用)
m.insert(make_pair(2, 20));
//第三种插入方式
m.insert(map<int, int>::value_type(3, 30));
//第四种插入方式(常用)
m[4] = 40;
//第五种插入方式(常用,但需要现代C++11)
m.insert({5, 50});
printMap(m);
//删除
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
//清空
m.erase(m.begin(),m.end());
m.clear();
printMap(m);
}
int main() {
test01();
return 0;
}
運行結果:
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 50
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
key = 5 value = 50
key = 2 value = 20
key = 4 value = 40
key = 5 value = 50
總結:
- map插入方式很多,記住其一即可
- 插入 --- insert
- 刪除 --- erase
- 清空 --- clear
map查找和統計
功能描述:
- 對map容器進行查找數據以及統計數據
函數原型:
find(key);//查找key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();count(key);//統計key的元素個數
示例:
#include <iostream>
#include <map>
using namespace std;
//查找和统计
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
//查找
map<int, int>::iterator pos = m.find(3); //将pos指向3 30这个元素
if (pos != m.end()) //表示没找到元素
{
cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = m.count(3);
cout << "num = " << num << endl;
}
int main() {
test01();
return 0;
}
運行結果:
找到了元素 key = 3 value = 30
num = 1
總結:
- 查找 --- find (返回的是迭代器)
- 統計 --- count (對於map,結果為0或者1)
map容器排序
學習目標:
- map容器默認排序規則為 按照key值進行 從小到大排序,掌握如何改變排序規則
主要技術點:
- 利用仿函數,可以改變排序規則
示例:
#include <iostream>
#include <map>
using namespace std;
class MyCompare {
public:
bool operator()(int v1, int v2) const{
return v1 > v2;
}
};
void test01()
{
//默认从小到大排序
//利用仿函数实现从大到小排序
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
int main() {
test01();
return 0;
}
運行結果:
key:5 value:50
key:4 value:40
key:3 value:30
key:2 value:20
key:1 value:10
總結:
- 利用仿函數可以指定map容器的排序規則
- 對於自定義數據類型,map必須要指定排序規則,同set容器