1. 程式人生 > 實用技巧 >STL—map/multimap容器

STL—map/multimap容器

map/ multimap容器

1 map基本概念

簡介:

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

本質:

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

優點:

  • 可以根據key值快速找到value值

map和multimap區別

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

2 map構造和賦值

功能描述:

  • 對map容器進行構造和賦值操作

函式原型:

構造:

  • map<T1, T2> mp; //map預設建構函式:
  • map(const map &mp); //拷貝建構函式

賦值:

  • map& operator=(const map &mp); //過載等號操作符

示例:

#include <map>

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

	system("pause");

	return 0;
}

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

3 map大小和交換

功能描述:

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

函式原型:

  • size(); //返回容器中元素的數目
  • empty(); //判斷容器是否為空
  • swap(st); //交換兩個集合容器

示例:

#include <map>

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

	system("pause");

	return 0;
}

總結:

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

4 map插入和刪除

功能描述:

  • map容器進行插入資料和刪除資料

函式原型:

  • insert(elem); //在容器中插入元素,這裡的元素指的是對組。
  • clear(); //清除所有元素
  • erase(pos); //刪除pos迭代器所指的元素,返回下一個元素的迭代器。
  • erase(beg, end); //刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
  • erase(key); //刪除容器中值為key的元素。

示例:

#include <map>

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

    //這裡3代表的是key值
	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

5 map查詢和統計

功能描述:

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

函式原型:

  • find(key); //查詢key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();
  • count(key); //統計key的元素個數

示例:

#include <map>

//查詢和統計
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;
	}

	//統計
    //map中不允許插入key值相同的對組,所以count函式執行的結果只有兩個,要麼是1,要麼是0
	int num = m.count(3);
	cout << "num = " << num << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

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

6 map容器排序

學習目標:

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

主要技術點:

  • 利用仿函式,可以改變排序規則

示例:

#include <map>

class MyCompare {
public:
	bool operator()(int v1, int v2) {
		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();

	system("pause");

	return 0;
}

總結:

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