map的常用用法

map翻譯爲映射,也是常用的STL容器,可以將任何基本類型映射到任何基本類型。

需要添加==#include==頭文件。

定義

map<typename1,typename2>mp;

第一個是鍵的類型,第二個是值的類型。

注意:如果是字符串到整型的映射,必須使用string而不能使用char數組。

容器內元素的訪問

1.下標訪問

注意:map中的鍵是唯一的。

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['c']=20;
	mp['c']=30;//20被覆蓋
	cout<<mp['c']; 
	return 0;
 } 

2.迭代器訪問

map<typename1,typename2>::iterator it;

事實上,map可以使用it->first來訪問鍵,使用it->second來訪問值。

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

注意:map內部是使用紅黑樹實現的(set也是),在建立映射的過程中會自動實現從小到大的排列。

map常用函數實例解析

1.find()

find(key)返回鍵爲key的映射的迭代器,時間複雜度爲O(logN),N爲map中映射的個數。

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	map<char,int>::iterator it=mp.find('r');
	cout<<it->first<<" "<<it->second<<endl;
	return 0;
}

2.erace()

(1)刪除單個元素

mp.erase(it)

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	map<char,int>::iterator it=mp.find('r');
	mp.erase(it);//刪除r 30
	for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

mp.erase(key)

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	//map<char,int>::iterator it=mp.find('r');
	mp.erase('r');
	for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

(3)刪除區間內的所有元素

mp.erase(first,last),刪除左閉右開的區間[first,last).

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	map<char,int>::iterator it=mp.find('m');
	mp.erase(it,mp.end());//刪除 r 30和m 20 
	for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

(4)size()

size用來獲得map中映射的對數,時間複雜度爲O(1)。

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	/*map<char,int>::iterator it=mp.find('m');
	mp.erase(it,mp.end());//刪除 r 30和m 20 
	for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}*/
	cout<<mp.size(); 
	return 0;
}

(4)clear()

clear用來清空map中的所有元素,複雜度爲O(N),其中N爲map中元素的個數。

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<char,int>mp;
	mp['m']=20;
	mp['r']=30;
	mp['a']=40;
	/*map<char,int>::iterator it=mp.find('m');
	mp.erase(it,mp.end());//刪除 r 30和m 20 
	for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		cout<<it->first<<" "<<it->second<<endl;
	}*/
	mp.clear();
	cout<<mp.size(); 
	return 0;
}

map的常見用途

  1. 需要建立字符(或字符串)與整數之間映射的題目,使用map可以減少代碼量。
  2. 判斷大整數或者其他類型數據是否存在的題目,可以把map當bool數組用。
  3. 字符串和字符串的映射 可能也會用到。
發佈了59 篇原創文章 · 獲贊 14 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章