当前位置:网站首页>A topic of map
A topic of map
2022-08-04 10:44:00 【i run】
最近学到了map,为了熟悉使用,Find a topic to practice my hand,Topic for cow from:KY264 单词识别
目录
To realize the dictionary order:
Implementation according to the number to the descending order of
题目:
要求:
1. Will sentence the words according to occurrences descending order
2. The same number of words,按照字典序排列(升序排列)
3. 不区分大小写
4. To recognize words and an end
5. Enter a word and a period,Don't consider other symbols
样例:
The sample is worth we noticed is:
开头的大写字母 A ,When the output is in accordance with lowercase letters to
So when we are in the output will be subject to lower case----大写改小写
思路
1. To replace a word in all capital letters with lower case letters
2. 将每个单词插入到map对象中,完成计数+字典序(默认为升序),符合要求
3. In times aspair类型中的 key 值,单词作为 value 值,插入到 multimap 对象中,Complete the descending order
代码实现
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
string str;
map<string, int> mp;
while (getline(cin, str))
{
int slow = 0; //Words starting subscript
string s; //存放单个单词
for (int i = 0; i < str.size(); i++)
{
//大写变小写
if ('A' <= str[i] && str[i] <= 'Z')
{
str[i] += 32;
}
//Take the word into themp中,插入+计数+Automatic descending order
if (str[i] == ' ' || str[i] == '.')
{
int num = i - slow; //The word size
s = str.substr(slow, num); //Through the word starting subscript out and size
mp[s]++;
slow = i + 1; //Skip Spaces to the starting position of the next word
}
}
//The above has been dictionary sequence arranged in a row---The following will be the large number of in front
//Note number appear equal,To prevent heavy,要用multimap
//Manual changes to descending order
multimap<int, string, greater<int>> intsort;
for (const auto& e : mp)
{
//插入+排序
intsort.insert(make_pair(e.second, e.first));
}
for (const auto& e : intsort)
{
cout << e.second << ":" << e.first << endl;
}
}
return 0;
}
Analysis of how to realize the above:
To realize the dictionary order:
创建 map 对象 mp,单词作为 pair 类型的 key 值,次数作为 pair 类型的 value 值
输入一句话,while中用 getline 获取字符串,防止直接用 cin 遇到 ‘ ’ Stop getting directly
s 存放单个单词、slow Record each word starting subscript
for 循环下标遍历,Will change all capital letters is lower case
遇到 ' ' 或者 ‘ . ’ ,That must have in front of the word,This time you can to pick up the words:
The word is equal to the substring,我们用到 substr,num 为The word size,slowFor the word starting subscript
通过 i - slow,就可以算出The word size
s 存储单词,用 s Insert the words to mp 中,并++,计数
最后要更新下一个单词的起始位置, slow = i + 1,Skip Spaces to the starting position of the next word
Implementation according to the number to the descending order of
这里用到 multimap ,multi Version allows keys redundant,次数作为 pair 类型的 key 值,单词作为 pair 类型的 value 值
将上述 pair Types of key/value pair is inserted into the multimap 的对象中,完成排序
注意:无论是map还是multimap,The default are compare key 值进行升序排列,Now in order to realize descending,Need to manually change the copy function
multimap<int, string, greater<int>> intsort;
将 mp 对象的 first 和 second 交换位置,构建 intsort 对象的 pair
for (const auto& e : mp)
{
//插入+排序
intsort.insert(make_pair(e.second, e.first));
}
插入完成,Sorting is done,此时 intsort Object is stored in the output key/value pair is we need to:
for (const auto& e : intsort)
{
cout << e.second << ":" << e.first << endl;
}
In accordance with the requirements for the output of the sample:Words in the front,The number in the,Output the entire content,这个题目就完成了.
边栏推荐
猜你喜欢
利用pytest hook函数实现自动化测试结果推送企业微信
第二批养老理财试点产品发行 一小时销售20亿元
MySQL core SQL: SQL structured query statements, library, table operation, CRUD
HTB-Sense
语音社交app源码——具备哪些开发优势?
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss
STM32前言知识总结
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
Jina 实例秀|基于神经搜索的网络安全威胁检测(一)
Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
随机推荐
rk3399-339 usb设备复合 总体流程
LVS+Keepalived群集部署
SVG 的 path 属性绘制图形
What is the principle of thermal imaging temperature measurement?Do you know?
Super Learning Method
mysql进阶(二十六)MySQL 索引类型
RL78开发环境
sqlilabs less-38~39
JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
【励志】复盘的重要性
STM32入门开发 制作红外线遥控器(智能居家-万能遥控器)
Introduction to the core methods of the CompletableFuture interface
iMeta | 德国国家肿瘤中心顾祖光发表复杂热图(ComplexHeatmap)可视化方法
MATLAB程序设计与应用 3.2 矩阵变换
RAID介绍及RAID5配置实例
华为云安全云脑,让企业云化运营更放心
视频加密怎么播放_win播放器加密视频
MATLAB程序设计与应用 3.1 特殊矩阵
datax oracle to oracle增量同步
【Idea series】idea configuration