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对象,丢入队列 ...
随机推荐
- tfs项目解绑及svn上传
1.tfs解绑 file--源代码管理——tfs解绑 2.svn将本地的文件夹上传到server 右击--import--url--新建文件夹
- lnmp搭建环境易错误点
1.虚拟主机使用桥接网络 2.nginx配置server 3.先ping通,service iptables stop 4.php-fpm开启,使之能正确解析php文件,nginx开启 5.mysql ...
- Linux基础命令---lsusb
lsusb 显示本机的usb设备列表,可以显示出usb的详细信息,包括设备的读取速度和描述符.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE ...
- mysql合并 两个count语句一次性输出结果的方法
mysql合并 两个count语句一次性输出结果的方法 需求场景:经常要查看有两个表统计数,用SELECT COUNT(*) FROM hotcontents,SELECT COUNT(*) FROM ...
- Linux中Postfix邮件接收配置(四)
Dovecot介绍 MRA邮件取回代理也有很多如courier-imap,cyrus-imap和dovecot这三个个工具,下面重点介绍Dovecot: 1.高安全性.据 Dovecot 的作者声称, ...
- Contiki源码+原理+功能+编程+移植+驱动+网络(转)
源:Contiki源码+原理+功能+编程+移植+驱动+网络 请链接:http://www.rimelink.com/nd.jsp? id=31&_np=105_315 假设您对于用Contik ...
- 20145329 《网络对抗技术》Web基础
实践目标 Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. Web前端javascipt 理解JavaScript的基本 ...
- VC++使用服务做守护进程的示例(转载)
转载:http://blog.csdn.net/zdy0_2004/article/details/40461571 #define _WIN32_WINNT 0x502 #define _CRT_S ...
- Python3基础 issubclass 判断基类
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- C++写入mbr
#include <windows.h> #include <winioctl.h> unsigned char scode[] = "\xb8\x12\x00\xc ...