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中进行统计

  1. /*
  2. map就是键到值得一个映射
  3.  
  4. 分析:把每个单词"标准化",即全部转化为小写字母后再进行排序,然后再放到map中进行统计
  5.  
  6. 其实真的挺简单的,就是一个map的用法。
  7.  
  8. */
  9. #include <iostream>
  10. #include <string>
  11. #include <cctype>
  12. #include <vector>
  13. #include <map>
  14. #include <algorithm>
  15. using namespace std;
  16.  
  17. map<string,int> cnt;//用来单词对应的标准化后的次数
  18. vector<string> words;//用来存每个单词
  19.  
  20. //将单词进行标准化
  21. string repr(const string& s){
  22. string ans=s;
  23. for(int i=;i<ans.length();i++){
  24. ans[i]=tolower(ans[i]);//大写边小写
  25. }
  26. sort(ans.begin(),ans.end());//将字母排序
  27. return ans;
  28. }
  29.  
  30. int main(){
  31. freopen("in.txt","r",stdin);
  32. int n=;
  33. string s;
  34. while(cin>>s){//读入单词
  35. if(s[]=='#') break;//输入结束标志
  36. words.push_back(s);//单词插入words的vector中
  37. string r=repr(s);//标准化:大写变小写,字母排序
  38. if(!cnt.count(r)) cnt[r]=;//,判断键出现过没有,统计次数
  39. cnt[r]++;
  40. }
  41. vector<string> ans;//用来储存符合题目要求的单词
  42. for(int i=;i<words.size();i++){
  43. if(cnt[repr(words[i])]==) ans.push_back(words[i]);//判断单词对应的标准化出现的次数
  44. }
  45. sort(ans.begin(),ans.end());//对结果单词进行排序
  46. for(int i=;i<ans.size();i++){
  47. cout<<ans[i]<<"\n";//输出答案
  48. }
  49. return ;
  50. }

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. kendo grid应用经验总结

    学习网址 https://docs.telerik.com/kendo-ui/controls/editors/dropdownlist/overview https://demos.telerik. ...

  2. Object之wait

    一.源码. 1.公共本地,且可继承但不可重写. 2.公共,且可继承但不可重写. 3.公共,且可继承但不可重写. 二.解释. 1.因为这些方法是Object类中的非静态的public方法,而任何其他类都 ...

  3. npm包上传下载的命令及例子

    npm包上传下载的命令及例子. 新建hello.js 执行:npm init 执行:npm adduser ( username:XXX password:XXX email:XXX ) 上传:npm ...

  4. VS2010/MFC编程入门之三十五(菜单:菜单及CMenu类的使用)

    鸡啄米在上一节中讲的是VS2010的菜单资源,本节主要讲菜单及CMenu类的使用. CMenu类的主要成员函数 MFC为菜单的操作提供了CMenu类,下面鸡啄米就常用的几个成员函数进行简单的介绍. B ...

  5. lnmp之nginx1.10.2安装

    linux下nginx的安装 为了后面避免缺失,还是什么都安装一下(后面安装php和mysql就不需要重复再执行下面这个了,当然你再执行一遍也没问题) [root@localhost src]# yu ...

  6. kafka监控工具之一--kafka-manager

    部署环境 jdk7 kafka_2.10-0.9.0.1 xshell4 rhel-server-6.5 kafka-manager 是功能比较多的kafka管控工具. 安装方法一 安装方法二 步骤一 ...

  7. Linux服务器---设置服务启动

    设置服务开关 用户可以设置某项服务开机启动或者关闭,有图形界面和命令两种方式 1.图形界面 1)在终端输入命令setup,在弹出的界面选择“系统服务” 2)也可以直接在终端输入命令“ntsysv”,得 ...

  8. thinkphp标签实现bootsrtap轮播carousel实例

    thinkphp标签实现bootsrtap轮播carousel实例由于轮播carousel第一个div需要设置active样式才能正常显示,上面的圆点也同样需要数字,使用volist标签在循环的同时可 ...

  9. Python入门之字典的操作详解

    这篇文章主要介绍了Python 字典(Dictionary)的详细操作方法,需要的朋友可以参考下: Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一 ...

  10. zabbix-server新增zabbix-agent

    zabbix监控系统搭建好了之后,就需要为各种角色host加入进来,现在新增一台zabbix-agent: 1.在172.16.23.128上安装zabbix-agent,zabbix-server: ...