STL學習之map

這一版學習的專題爲map,一個非常好用的容器。它能夠將關鍵字與另外的不同的關鍵字行成一一對應的關係,是很好的離散化的容器(不要打我臉),在某些時候能夠派上很大的用處。

map的簡單介紹

map<const Key,Data>M

這條語句構成的map是將Key與Data類型的數據一一對應起來,比如map<const char*,int>。

<pre name="code" class="cpp">struct com{
	bool operator ()(const char* a,const char* b){return strcmp(a,b)<0;}
};
int main()
{
	map<const char*,int,com>M;
	M["Triangle"]=180;
	M["Rectangle"]=360;
	M["Pentagon"]=540;
	printf("%d\n%d\n%d\n",M["Triangle"],M["Rectangle"],M["Pentagon"]);
}


上面這段程序會輸出:

180

360

540

當然還有其他插入數據的方法。

struct com{
<span style="white-space:pre">	</span>bool operator ()(const char* a,const char* b){return strcmp(a,b)<0;}
};
int main()
{
<span style="white-space:pre">	</span>map<const char*,int,com>M;
<span style="white-space:pre">	</span>M["Triangle"]=180;
<span style="white-space:pre">	</span>M.insert(make_pair("Rectangle",360));
<span style="white-space:pre">	</span>M.insert(map<const char*,int>::value_type("Pentagon",540));
<span style="white-space:pre">	</span>printf("%d\n%d\n%d\n",M["Triangle"],M["Rectangle"],M["Pentagon"]);
}
結果是一樣的,不過我更喜歡第一和第二種,第三種太難記了。

在map中查找需要用到迭代器。

<span style="white-space:pre">	</span><pre name="code" class="cpp"><span style="white-space:pre">	</span>map<const char*,int>::iterator i;
	i=M.find("Triangle");
	if(i!=M.end())
		printf("Found it\n");
	else
		printf("Didn't find it\n");


使用find函數,如果找到了目標,返回指向目標的指針;否則返回end()

在map中刪除某個元素:

<span style="white-space:pre">	</span>map<const char*,int>::iterator i;
<span style="white-space:pre">	</span>i=M.find("Triangle");
<span style="white-space:pre">	</span>if(i!=M.end())
<span style="white-space:pre">		</span>printf("Found it\n"),M.erase(i);
<span style="white-space:pre">	</span>else
<span style="white-space:pre">		</span>printf("Didn't find it\n");
<span style="white-space:pre">	</span>i=M.find("Triangle");
<span style="white-space:pre">	</span>if(i!=M.end())
<span style="white-space:pre">		</span>printf("Found it\n"),M.erase(i);
<span style="white-space:pre">	</span>else
<span style="white-space:pre">		</span>printf("Didn't find it\n");

map的遍歷:

map<const char*,int>::iterator i;
	for(i=M.begin();i!=M.end();i++)
		printf("%s %d\n",i->first,i->second);
由於map自動按key升序排序,不能用sort。

還有就是一些成員函數:

begin()     //返回指向map頭部的迭代器

end()       //返回指向map末尾的迭代器

clear()     //刪除map中所有元素

empty()   //如果map爲空則返回true

count()    // 返回指定元素出現的次數(然而無論什麼元素都只會出現一次,所以可以判斷該元素是否出現過)

erase()   //刪除一個元素

find()      //查找一個元素

insert()   //插入元素

lower_bound()    //返回鍵值>=給定元素的第一個位置

upper_bound()   //返回鍵值>給定元素的第一個位置(下上相減可以得到該元素的個數,然而並沒有什麼用)

max_size()       //返回可以容納的最大元素個數

rbegin()         //返回一個指向map尾部的逆向迭代器

rend()           //返回一個指向map頭部的逆向迭代器

size()           //返回map中元素的個數

swap()        //交換兩個map

附上一個例題:

傳送門:http://www.cqoi.net:2012/problem.php?id=2001

code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define LL long long
#define MAXN 100000
using namespace std;
struct com{
    bool operator ()(const LL a,const LL b)const{return a<b;}
};
map<const LL,LL,com>hash;
int n,cnt;
LL a[MAXN],b[MAXN];
int main()
{
    int i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    memcpy(b,a,sizeof b);
    sort(a,a+n);
    hash[a[0]]=++cnt;
    for(i=1;i<n;i++)
        if(a[i]!=a[i-1])
            hash[a[i]]=++cnt;
    for(i=0;i<n;i++)
        printf("%lld\n",hash[b[i]]);
}


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