当前位置:网站首页>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,这个题目就完成了.
边栏推荐
猜你喜欢

HTB-Sense

What is the principle of thermal imaging temperature measurement?Do you know?

onlyoffice设置跟踪变化trackChanges默认为对自己启动

AWS Lambda related concepts and implementation approach

map的一道题目<单词识别>

解决:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING

zabbix deployment

Introduction to Mysql storage engine

mae,mse,rmse分别利用sklearn和numpy实现

sqlilabs less-40
随机推荐
usb设备复合g_webcam摄像头码流传输功能以及g_serial串口功能
双向带头循环链表实现
JS工厂模式_工厂模式进行封装
zabbix deployment
为企业数字化转型提供服务_数字赋能企业转型
在 .NET MAUI 中如何更好地自定义控件
移动端 开源低代码工具 beeware 和 kivy
【cookie 临时存储数据,WebStorage ,sessionStorage】
无代码平台数字入门教程
无代码平台多行文字入门教程
WPF 截图控件之画笔(八)「仿微信」
RL78 development environment
Business collocations
利用pytest hook函数实现自动化测试结果推送企业微信
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
二叉树的基础练习
江西发布紧急通知:全面开展涉校涉生安全隐患大排查
pyvista 的介绍与使用