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. 160901、在大型项目中组织CSS

    编写CSS容易. 编写可维护的CSS难. 这句话你之前可能听过100次了. 原因是CSS中的一切都默认为全局的.如果你是一个C程序员你就知道全局变量不好.如果你是任何一种程序员,你都知道隔离和可组合的 ...

  2. scala 隐式转换

    先参考这篇文章:http://www.jianshu.com/p/a344914de895 package com.test.scalaw.test /** * scala隐式转换 */ object ...

  3. iOS 学习笔记 一 (2015.02.05)

    一:Xcode6输入框设置为 keyboard type设置为Number Pad弹不出键盘的解决办法   问题:Can't find keyplane that supports type 4 fo ...

  4. windows cmd command line 命令

    Reference: 1. http://msdn.microsoft.com/en-us/library/ms977170.aspx(前面有一段VB看不懂没关系) 2. http://csserve ...

  5. css 样式设计(一)( 在线150个例子 | 背景 | 文本 | 字体 | 链接 | 列表 | 表格 | 盒模型 | 边框 | 轮廓 | 边距 | 填充 |分组和嵌套 | 尺寸 | 定位 | 浮动 |对齐 )

    一.css在线150个例子 http://www.w3cschool.cc/css/css-examples.html 二.背景图片水平方向重复 : body { background-image:u ...

  6. java UUID

    UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符.UUID具有以下涵义: 经由一定的算法机器生成 为了保证 ...

  7. java 同步锁方法

    方法一:动态同步锁 class Demo_thread implements Runnable{ public static int sum = 0; public synchronized void ...

  8. 【20160924】GOCVHelper综述

    GOCVHelper(GreenOpen Computer Version Helper )是我在这几年编写图像处理程序的过程中积累下来的函数库.主要是对Opencv的适当扩展和在实现Mfc程序时候的 ...

  9. Oracle 客户端安装配置

    电脑上安装了Oracle11G,我远程导出一个10g的数据库数据时,报了错误,猜测可能是我的11G客户端版本的问题.所以下载了10G的客户端 安装. 其实客户端的配置读取的是两个文件监听配置文件lis ...

  10. CentOS 安装SolrCloud

    1.什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候是不 ...