map--概述:

  映射(Map)和多重映射(Multimap)是基于某一类型Key的键集的存在,提供对TYPE类型的数据进行快速和高效的检索。
l对Map而言,键只是指存储在容器中的某一成员。
lMultimap允许重复键值,Map不允许。
lMap和Multimap对象包涵了键和各个键有关的值,键和值的数据类型是不相同的,这与Set不同。
Map内部数据的组织是一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在Map内部所有的数据Key都是有序的。
 
 

map c

产生一个空的map/multimap,其中不含任何元素

map   c (op)

以op为排序准则,产生一个空的map/multimap

map   c1(c2)

产生某个map/multimap的副本,所有元素均被复制

map   c (beg, end)

以区间[beg;   end]内的元素产生一个map/multimap

map   c (beg, end, op)

以op为排序准则,利用[beg;   end]内的元素生成一个map/multimap

c.~map()

销毁所有元素,释放内存

 
元素的访问
1.定义迭代器(iterator):map<string,float>::iterator pos;
l其中map<string, float>表明这个迭代器的类型,声明一个迭代器pos,迭代器的角色类似于C/C++中的指针。
2.当迭代器pos指向map容器中某个元素:
l表达式pos->first获得该元素的key;
l表达式pos->second获得该元素的value。
 
题目:
(会陆续添加)
 1.不能再裸啦! 再裸就不见一丝啦!
 #include<iostream>
#include<map>
using namespace std; int main()
{
int n, ans, a, T;
cin>>T;
while(T--)
{
ans = ;
scanf("%d", &n);
map<int,int> m;
for(int i=; i<n; i++)
{
scanf("%d",&a);
m[a]++;
if(m[a]>ans)
ans=m[a];
}
printf("%d\n", ans); }
return ;
}

3.裸裸裸裸,,,,,,,,,,切着玩吧!

http://acm.hdu.edu.cn/showproblem.php?pid=1800

#include<iostream>
#include<cstdio>
#include<map>
using namespace std; int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
int i, max =-, q;
map<int, int> M;
for(i=; i<n; i++)
{
scanf("%d", &q);
M[q]++;
if(max<M[q])
max=M[q];
}
printf("%d\n", max);
}
return ;
}

2.我就是喜欢“裸体”。 来一发!

 #include <iostream>
#include <map>
#include <algorithm>
using namespace std; int main()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
int n;
while (cin>>n && n)
{
map <string, int> Balloon;
string s;
for (int i=; i<n; i++)
{
cin>>s;
Balloon[s]++;
}
int iMax = ;
map <string, int>::iterator point, loc;
for (point=Balloon.begin(); point!=Balloon.end(); point++)
if (iMax<point->second)
{
iMax = point->second;
loc = point;
}
cout<<loc->first<<endl;
}
return ;
}

3.此题较难一点, 草滩小恪读懂题意都很费劲(英语渣的悲哀!哭,哭,哭,)。

#include<iostream>
#include<map>
#include<set>
using namespace std; typedef map<int, multiset<int> >line;
map<int, multiset<int> >mx;
map<int, multiset<int> >my; int bomb(line &x, line &y, int pos)
{
int ret = x[pos].size();
multiset<int>::iterator it;
for(it=x[pos].begin(); it!=x[pos].end(); it++)
y[*it].erase(pos);
x[pos].clear();
return ret;
} int main()
{
int n, m, c, d, tx, ty;
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==&&m==) break;
mx.clear();
my.clear();
for(int i=; i<n; i++)
{
scanf("%d%d", &tx, &ty);
mx[tx].insert(ty);
my[ty].insert(tx);
}
for(int i=; i<m; i++)
{
scanf("%d%d", &c, &d);
int ans;
if(c==) ans = bomb(mx, my, d);
else ans = bomb(my, mx, d);
printf("%d\n", ans);
}
printf("\n");
}
return ;
}

4.这个题不太难, 就是需要些巧妙地读入处理技巧。

http://acm.hdu.edu.cn/showproblem.php?pid=1075

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std; map<string,string>mp;
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
mp.clear();
string str1,str2;
cin>>str1;
while(cin>>str1)
{
if(str1=="END")break;
cin>>str2;
mp[str2]=str1;
}
cin>>str1;
char ch;
ch=getchar();
str1="";
while()
{
while()
{
scanf("%c",&ch);
if(!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')))break;
str1+=ch;
}
if(str1=="END")break;
if(mp.find(str1)==mp.end())cout<<str1;
else cout<<mp[str1];
str1="";
printf("%c",ch);
}
return ;
}

但是, 如果这道题的单词数据很大, 那么上面的代码就不行啦, 虽然map是基于红黑树的, 在处理重复单词时并没体现出特别的高效。 一个很好的方法就是--字典树!

STL--map的更多相关文章

  1. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  2. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  3. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  4. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  5. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  6. STL MAP 反序迭代

    ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...

  7. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  8. Dictionary,hashtable, stl:map有什么异同?

    相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...

  9. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

  10. C++ STL map使用

    Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map构造函数:map<string , in ...

随机推荐

  1. 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序

    我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...

  2. 【Pro ASP.NET MVC 3 Framework】.学习笔记.3.MVC的主要工具-单元测试

    IProductRepository接口定义了一个仓库,我们通过它获得.更新Product对象.IPriceReducer接口指定了一个功能,它将要对所有的Products实施,通过一个参数,降低他们 ...

  3. sqlite3内存不断增加的原因

    数据库是这样设计的:用内存保存数据,以提高增删查改的速度,同时把数据写入磁盘,让数据落地. 如果不删除数据库里的数据,随着数据不断地添加到数据库,数据库越来越大,RES内存也越来越大. 见测试代码a. ...

  4. codeigniter db操作方法

    链接数据库 ——- $this->load->database();//手动连接数据库 //连接多数据库 $DB1 = $this->load->database(‘group ...

  5. Android处理图片OOM的若干方法小结 (推荐)

    众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视平台而定).因此在开发应用时需要特别关注自身的内存使用量,而一般最耗内存量的资源,一般是图片.音频文 ...

  6. http://www.bejson.com/go.html?u=http://www.bejson.com/demo2.html

    json 解析工具http://www.bejson.com/go.html?u=http://www.bejson.com/demo2.html

  7. java静态块

    一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的 静态代码块的初始化顺序  class Parent{ static String name = &q ...

  8. easyui tab 关闭

    <div id="mm" class="easyui-menu" style="width:150px;">        &l ...

  9. python中字符串\r的奇怪问题

    示例: 我这里有一字符串: u'北京市工商行政管理局大兴分局\r <a onclick="showJDS(\'fa641bb3be5b44a1b618433833982fee\',\' ...

  10. google prettify 代码高亮显示

    引入js和css文件 下载地址 http://files.cnblogs.com/jaday/prettify.zip js文件代码 !function(){var q=null;window.PR_ ...