天天看點

map和multimap的詳解(C++)1.map和multimap基本概念:2.map構造和指派: 3.map大小和交換: 4.map插入和删除 5.map查找和統計:6.map容器排序:7.不會自動排序的unordered_map

目錄

1.map和multimap基本概念:

1)簡介: 

2)本質:

3)map和multimap差別: 

2.map構造和指派: 

1)功能描述:

2)構造:

3)指派:

4)代碼示例:

5)  總結:

3.map大小和交換: 

1) 功能描述:

2) 函數原型: 

3) 示例代碼: 

4.map插入和删除 :

1) 功能描述:

2) 函數原型:

3) 代碼示例:

4) 總結:

5.map查找和統計:

1) 功能描述:

2) 函數原型:

3) 代碼示例:

​4) 總結:

6.map容器排序:

7.不會自動排序的unordered_map

1.map和multimap基本概念:

1)簡介: 

  • map中所有元素都是pair
  • pair中第一個元素為key(鍵值),起到索引作用,第二個元素為value(實值)
  • 所有元素都會根據元素的鍵值自動排序

2)本質:

map/multimap屬于關聯式容器,底層結構是用二叉樹實作。

3)map和multimap差別: 

  • map不允許容器中有重複key值元素
  • multimap允許容器中有重複key值元素

2.map構造和指派: 

1)功能描述:

對map容器進行構造和指派操作

 2)構造:

  • map<T1, T2> mp;

     //map預設構造函數:
  • map(const map &mp);

     //拷貝構造函數

3)指派:

  • map& operator=(const map &mp);

     //重載等号操作符

4)代碼示例:

#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);
 
	cout << (m3.find(3))->second << endl;
}
 
int main() {
	test01();
	system("pause");
	return 0;
}
           

 運作結果:

map和multimap的詳解(C++)1.map和multimap基本概念:2.map構造和指派: 3.map大小和交換: 4.map插入和删除 5.map查找和統計:6.map容器排序:7.不會自動排序的unordered_map

5)總結:

map中所有元素都是成對出現,插入資料時候要使用對組 pair<T,T> 

3.map大小和交換: 

1)功能描述:

統計map容器大小以及交換map容器

2)函數原型: 

  • size();

        //傳回容器中元素的數目
  • empty(); 

     //判斷容器是否為空
  • swap(st);

     //交換兩個集合容器

3)示例代碼: 

#include<iostream>
#include<map>
#include<string>
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);
 
	cout << ((m.empty()) ? "m為空" : "m不為空,m的大小為:" +to_string(m.size())) << endl;
	cout << endl;
 
 
	map<int, int>m2;
	m2.insert(pair<int, int>(4, 2));
	m2.insert(pair<int, int>(5, 3));
	m2.insert(pair<int, int>(6, 4));
	printMap(m2);
 
	cout << "交換後:" << endl;
	m.swap(m2);
	printMap(m);
	printMap(m2);
}
 
int main() {
	test01();
 
	system("pause");
	return 0;
}
           

 運作結果:

map和multimap的詳解(C++)1.map和multimap基本概念:2.map構造和指派: 3.map大小和交換: 4.map插入和删除 5.map查找和統計:6.map容器排序:7.不會自動排序的unordered_map

 4) 總結:

  • 統計大小 --- size
  • 判斷是否為空 --- empty
  • 交換容器 --- swap

4.map插入和删除 

1)功能描述:

map容器進行插入資料和删除資料

2)函數原型:

insert(elem);           //在容器中插入元素。

clear();       //清除所有元素

erase(pos);     //删除pos疊代器所指的元素,傳回下一個元素的疊代器。

erase(beg, end);  //删除區間[beg,end)的所有元素 ,傳回下一個元素的疊代器。

erase(key);      //删除容器中值為key的元素。 

3)代碼示例:

#include<iostream>
#include<map>
#include<string>
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));
	//第四種插入方式(一般通路時用這個比較好,若key值不存在就會建立)
	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);
}
 
int main() {
	test01();
 
	system("pause");
	return 0;
}
           

 運作結果:

map和multimap的詳解(C++)1.map和multimap基本概念:2.map構造和指派: 3.map大小和交換: 4.map插入和删除 5.map查找和統計:6.map容器排序:7.不會自動排序的unordered_map

4)總結:

  • map插入方式很多,記住其一即可(建議一、二種)
  • 插入 --- insert
  • 删除 --- erase
  • 清空 --- clear

5.map查找和統計:

1) 功能描述:

對map容器進行查找資料以及統計資料

2) 函數原型:

  • find(key);

     //查找key是否存在,若存在,傳回該鍵的元素的疊代器;若不存在,傳回set.end();
  • count(key);

     //統計key的元素個數

3)  代碼示例:

#include<iostream>
#include<map>
#include<string>
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));
 
	//查找
	map<int, int>::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;
}
 
int main() {
	test01();
 
	system("pause");
	return 0;
}
           

運作結果: 

map和multimap的詳解(C++)1.map和multimap基本概念:2.map構造和指派: 3.map大小和交換: 4.map插入和删除 5.map查找和統計:6.map容器排序:7.不會自動排序的unordered_map
 4)總結:

  • 查找 --- find (傳回的是疊代器)
  • 統計 --- count (對于map,結果為0或者1)

6.map容器排序:

仿函數和自定義函數更改預設排序規則和Value值排序 。

map容器預設排序規則為 按照key值進行 從小到大排序,掌握如何改變排序規則

仿函數:就是在一個類中重載括号運算符。 

代碼示例: 

#include<iostream>
#include<map>
#include<vector>
#include <algorithm>
 
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;
}
//仿函數
class MyCompare {
public://降序
	bool operator()(int v1, int v2) {
		return v1 > v2;
	}
};
//普通函數
bool myCompare(const pair<int, int>&p1, const pair<int, int>&p2) {
	return p1.second > p2.second;
}
 
void test01() {
	map<int, int, MyCompare>m;//注意沒括号,隻是類名
 
	m.insert(make_pair(1, 80));
	m.insert(make_pair(2, 10));
	m.insert(make_pair(3, 20));
	m.insert(make_pair(4, 90));
	m.insert(make_pair(5, 30));
	//key值比較結果輸出
	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key=" << it->first << "  value=" << it->second << endl;
	}

	cout << "……………………………………" << endl;

	//按value值比較
	vector<pair<int, int>>v;
	map<int, int>m1;
	m1[1] = 5;
	m1[2] = 4;
	m1[3] = 1;
	m1[4] = 2;
	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
		v.push_back(make_pair(it->first, it->second));
	}
	sort(v.begin(), v.end(), myCompare);
	for (vector<pair<int, int>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "key=" << it->first << "  value=" << it->second << endl;
	}
}
 
int main() {
	test01();
 
	system("pause");
	return 0;
}
           

運作結果:

map和multimap的詳解(C++)1.map和multimap基本概念:2.map構造和指派: 3.map大小和交換: 4.map插入和删除 5.map查找和統計:6.map容器排序:7.不會自動排序的unordered_map

 總結:

  • 利用仿函數可以指定map容器的排序規則
  • 對于自定義資料類型,map必須要指定排序規則,同set容器

7.不會自動排序的unordered_map

引頭檔案< unordered_map>,其他multiunordered_map,set也一樣,其他API基本就都一樣。

在這裡就不一一闡述了。