您的位置:首页 > 其它

STL学习之map

2015-09-13 09:41 381 查看
这一版学习的专题为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]]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: