C++STL2--map
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的更多相关文章
- mapreduce中一个map多个输入路径
		
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
 - .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
		
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
 - Java基础Map接口+Collections工具类
		
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
 - Java基础Map接口+Collections
		
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
 - 多用多学之Java中的Set,List,Map
		
很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...
 - Java版本:识别Json字符串并分隔成Map集合
		
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
 - MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析
		
在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...
 - MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
		
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
 - MapReduce剖析笔记之五:Map与Reduce任务分配过程
		
在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...
 - MapReduce剖析笔记之三:Job的Map/Reduce Task初始化
		
上一节分析了Job由JobClient提交到JobTracker的流程,利用RPC机制,JobTracker接收到Job ID和Job所在HDFS的目录,够早了JobInProgress对象,丢入队列 ...
 
随机推荐
- Junit4用法
			
序号 方法和描述 1 void assertEquals(boolean expected, boolean actual) 检查两个变量或者等式是否平衡 2 void assertTrue(bool ...
 - VS2010/MFC编程入门之四十二(MFC常用类:CString类)
			
上一节鸡啄米讲了分割窗口的有关知识,本节开始讲解MFC的一些常用类,先来说说CString类. CString类简介 CString类作为MFC的常用类,当之无愧.可以这样说,只要是从事MFC开发,基 ...
 - 填格子3*N的方框使用2*1的矩形进行填充
			
考虑每个位置的前一个状态 可以发现有 我们分别给他们编号 假设 现在填充到了i+1行,我们可以发现从i行可以通过填充转到i+1行的状态 第i行第j列表示 可以 从上一个转态 j 可以到达这个状态的j ...
 - 数据仓库基础(十二)Informatica组件(2)
			
本文转载自:http://www.cnblogs.com/evencao/p/3152708.html 1.Router Transformation:在filter 组件中只能写一个过滤条件.而在R ...
 - Yahoo数据仓库架构简介
			
1. Yahoo数据仓库的整体架构 Yahoo数据仓库在基础架构上由hadoop集群和Oracle集群组成,hadoop集群是一个计算平台,完成所有ETL数据处理过程:Oracle集群只是一个查询环境 ...
 - eclipse工具maven项目打包文件不是最新修改的
			
1.问题描述:maven项目,完成一些功能开发后,在进行打包更新服务器系统时,本地eclipse工具对文件打成jar包操作,更新到服务器上没有修改的效果,包括打包整个工程为一个war包,也是有同样问题 ...
 - Linux服务器配置---ftp配置
			
FTP其他配置 在配置文件中,还有一些关于vsftpd的其他设置,这里列出来 # Example config file /etc/vsftpd/vsftpd.conf # Allow anonymo ...
 - Winform 出现“Win已停止工作”解决方法
			
一.错误描述 错误名称如上.整体错误如下: Win已停止工作 二.解决方法: 1.请以管理员身份运行命令:“netsh winsock reset”,重启电脑后再次尝试运行本工具. 2.使用360安全 ...
 - Java系列笔记(0) - 目录和概述
			
笔者在开发过程中发现自己基础太薄弱,读书时除了系统学习了一下Java的基础语法和用法.一点简单的数据结构和设计模式之外,再无深入系统的学习,而工作中的学习也是东晃一枪西晃一枪,不够扎实和系统.想到一个 ...
 - 配置mysql主从数据库
			
来源地址:https://www.cnblogs.com/alvin_xp/p/4162249.html Mysql主从配置,实现读写分离 大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡 ...