C++STL2--map

一、心得

本质上就是数组,关联数组,map就是键到值得一个映射,并且重载了[]符号,所以可以像数组一样用

map<string,int> cnt;//前键后值,键就可以理解为索引

if(!cnt.count(r)) cnt[r]=0;//统计键值出现过没有

二、用法

1   头文件 
  #include   <map> 
  
  2   定义 
  map<string,   int>   my_Map; 
  或者是typedef     map<string,   int>   MY_MAP; 
  MY_MAP   my_Map; 
  
  3   插入数据 
  (1)   my_Map["a"]   =   1; 
  (2)   my_Map.insert(map<string,   int>::value_type("b",2)); 
  (3)   my_Map.insert(pair<string,int>("c",3)); 
  (4)   my_Map.insert(make_pair<string,int>("d",4)); 
  
  4   查找数据和修改数据 
  (1)   int   i   =   my_Map["a"]; 
            my_Map["a"]   =   i; 
  (2)   MY_MAP::iterator   my_Itr; 
            my_Itr.find("b"); 
            int   j   =   my_Itr->second; 
            my_Itr->second   =   j; 
  不过注意,键本身是不能被修改的,除非删除。 
  
  5   删除数据 
  (1)   my_Map.erase(my_Itr); 
  (2)   my_Map.erase("c"); 
  还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。 
  
  6   迭代数据 
  for   (my_Itr=my_Map.begin();   my_Itr!=my_Map.end();   ++my_Itr)   {} 
  
  7   其它方法 
  my_Map.size()               返回元素数目 
  my_Map.empty()       判断是否为空 
  my_Map.clear()           清空所有元素 
  可以直接进行赋值和比较:=,   >,   >=,   <,   <=,   !=   等等 
  
  更高级的应用查帮助去吧,^_^;

三、实例

题目:

Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different
orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this
attribute, no matter how you rearrange their letters, you cannot form another word. Such words are
called ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we are working; you might think
that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible
domain would be the entire English language, but this could lead to some problems. One could restrict
the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the
same domain) but NOTE is not since it can produce TONE.
Write a program that will read in the dictionary of a restricted domain and determine the relative
ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be
“rearranged” at all. The dictionary will contain no more than 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any
number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken
across lines. Spaces may appear freely around words, and at least one space separates multiple words
on the same line. Note that words that contain the same letters but of differing case are considered to
be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line
consisting of a single ‘#’.
Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram
in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always
be at least one relative ananagram.
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries
#
Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon

分析:

把每个单词"标准化",即全部转化为小写字母后再进行排序,然后再放到map中进行统计

 /*
map就是键到值得一个映射 分析:把每个单词"标准化",即全部转化为小写字母后再进行排序,然后再放到map中进行统计 其实真的挺简单的,就是一个map的用法。 */
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; map<string,int> cnt;//用来单词对应的标准化后的次数
vector<string> words;//用来存每个单词 //将单词进行标准化
string repr(const string& s){
string ans=s;
for(int i=;i<ans.length();i++){
ans[i]=tolower(ans[i]);//大写边小写
}
sort(ans.begin(),ans.end());//将字母排序
return ans;
} int main(){
freopen("in.txt","r",stdin);
int n=;
string s;
while(cin>>s){//读入单词
if(s[]=='#') break;//输入结束标志
words.push_back(s);//单词插入words的vector中
string r=repr(s);//标准化:大写变小写,字母排序
if(!cnt.count(r)) cnt[r]=;//,判断键出现过没有,统计次数
cnt[r]++;
}
vector<string> ans;//用来储存符合题目要求的单词
for(int i=;i<words.size();i++){
if(cnt[repr(words[i])]==) ans.push_back(words[i]);//判断单词对应的标准化出现的次数
}
sort(ans.begin(),ans.end());//对结果单词进行排序
for(int i=;i<ans.size();i++){
cout<<ans[i]<<"\n";//输出答案
}
return ;
}

C++STL2--map的更多相关文章

  1. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  2. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  3. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  4. Java基础Map接口+Collections

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  5. 多用多学之Java中的Set,List,Map

            很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...

  6. Java版本:识别Json字符串并分隔成Map集合

    前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...

  7. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  8. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  9. MapReduce剖析笔记之五:Map与Reduce任务分配过程

    在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...

  10. MapReduce剖析笔记之三:Job的Map/Reduce Task初始化

    上一节分析了Job由JobClient提交到JobTracker的流程,利用RPC机制,JobTracker接收到Job ID和Job所在HDFS的目录,够早了JobInProgress对象,丢入队列 ...

随机推荐

  1. 圆的k次面积并

    搬运别人的 https://vjudge.net/problem/SPOJ-CIRUT //china no.1 #pragma comment(linker, "/STACK:102400 ...

  2. Object-C-属性参数

    assign:默认参数setter 方法不会引起引用计数的变化 retain:setter方法首先释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的引用计数为1 copy:setter 方法首先 ...

  3. AO中的空间关系

    名词解释: Boundary(边界): 只有线和面才有边界.面的边界是指组成面的框架线:线的边界是指线的二个端点(即起点和终点,不包括中间部分的节点):点没有边界. Interior(内部): 除去边 ...

  4. 使用Jmeter测试Dubbo接口(参数设置篇)

    WebSocket接口需要下载dubbo插件才能使用 本次下载的版本为jmeter-plugins-dubbo-1.3.6,下载完成后jar文件放到\lib\ext目录下 由于工作需要,最近需要对du ...

  5. json-lib基础

    一.json-lib所需的jar包: json-lib.jar,commons-beanutils.jar,commons-collections.jar,commons-lang.jar,commo ...

  6. Mocha describe 生命周期

    1 describe('test', function() { 2 // 在本测试块的所有测试用例之前执行且仅执行一次 3 before(function() { 4 5 }); 6 // 在本测试块 ...

  7. 机器学习、深度学习以及人工智能正在快速演进(ML、DL、AI)

    机器学习.深度学习以及人工智能正在快速演进 机器学习.深度学习和人工智能(ML.DL和AI)是彼此相关的概念,他们正在改变不知多少行业,改变其自身管理模式,同时改变做出决策的方式.显然,ML.DL和A ...

  8. (四)github之Git的初始设置

    设置姓名与邮箱地址 这里的姓名和邮箱地址会用在git的提交日志之中,在github上公开git仓库时会随着提交日志一起公开. 有两种方式, 第一种,在git bash下设置 第二种, 通过直接编辑.g ...

  9. bzoj5470 / P4578 [FJOI2018]所罗门王的宝藏

    P4578 [FJOI2018]所罗门王的宝藏 设第$i$行上的值改变了$r1[i]$,第$j$列上的值改变了$r2[i]$ 显然密码$(i,j,c)=r1[i]+r2[j]$ 对于同一列上的两个密码 ...

  10. 什么是BFC?

    转载自知乎:https://zhuanlan.zhihu.com/p/25321647 一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: ...