当前位置:网站首页>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,这个题目就完成了.
边栏推荐
- 小程序容器加快一体化在线政务服务平台建设
- 第二批养老理财试点产品发行 一小时销售20亿元
- Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
- Small program containers accelerate the construction of an integrated online government service platform
- 低代码是开发的未来吗?浅谈低代码开发平台的发展现状及未来趋势
- Camunda整体架构和相关概念
- 无代码平台多项选择入门教程
- HCIP 第十七天
- 无代码平台描述文字入门教程
- Jenkins User Manual (1) - Software Installation
猜你喜欢
随机推荐
转转测试环境的标签域名实践
无代码平台多行文字入门教程
HTB-Sense
双向带头循环链表实现
Business collocations
STM32入门开发 制作红外线遥控器(智能居家-万能遥控器)
AWS Lambda related concepts and implementation approach
低代码是开发的未来吗?浅谈低代码开发平台的发展现状及未来趋势
深度学习100例 —— 卷积神经网络(CNN)天气识别
MySQL:完整性约束和 表的设计原则
RL78 development environment
Mobile open source low code tools beeware and kivy
ThreadLocal详细分析
《迁移学习导论》第2版,升级内容抢先看!
WPF 截图控件之画笔(八)「仿微信」
图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)
Jenkins User Manual (1) - Software Installation
ROI LTV CPA ECPM体系讲解
有12个球,其中11个重量相等,只有1个不一样,不知是轻还是重.用天平秤三次,找出这个球.
Small program containers accelerate the construction of an integrated online government service platform