c++STL常用容器之Map/Multimap容器——全面總結(附案例解析)(十九)map按value值進行比較以及仿函數更改默認排序規則

這裏有C++STL——全面總結詳細教程(附案例解析)(持續更新中)


目錄

map/ multimap容器

map基本概念

map構造和賦值

map大小和交換

map插入和刪除

map查找和統計

map容器排序

仿函數更改默認排序規則和Value值排序


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);

	cout << (m3.find(3))->second << endl;
}

int main() {
	test01();

	system("pause");
	return 0;
}

總結:map中所有元素都是成對出現,插入數據時候要使用對組。

 

 

map大小和交換

功能描述:

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

函數原型:

  • size();    //返回容器中元素的數目
  • empty();  //判斷容器是否爲空
  • swap(st); //交換兩個集合容器
#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;
}

總結:

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

 

map插入和刪除

功能描述:

  • map容器進行插入數據和刪除數據

函數原型:

  • insert(elem);           //在容器中插入元素。
  • clear();       //清除所有元素
  • erase(pos);     //刪除pos迭代器所指的元素,返回下一個元素的迭代器。
  • erase(beg, end);  //刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
  • erase(key);      //刪除容器中值爲key的元素。
#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));
	//第四種插入方式
	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插入方式很多,記住其一即可
  • 插入 --- insert
  • 刪除 --- erase
  • 清空 --- clear

 

 

 

 

map查找和統計

功能描述:

  • 對map容器進行查找數據以及統計數據

函數原型:

  • find(key); //查找key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();
  • count(key); //統計key的元素個數
#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;
}

總結:

  • 查找 --- find (返回的是迭代器)
  • 統計 --- count (對於map,結果爲0或者1)

 

 

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容器的排序規則
  • 對於自定義數據類型,map必須要指定排序規則,同set容器

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章