当前位置:网站首页>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,这个题目就完成了.
边栏推荐
- JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
- Why are all hotel bathrooms transparent?
- 自己实现一个枚举validation校验器
- 视频加密怎么播放_win播放器加密视频
- 关于ARM2440中断源个数的一点想法[通俗易懂]
- Introduction to Mysql storage engine
- 高级转录组分析和R数据可视化火热报名中(2022.10)
- 移动端 开源低代码工具 beeware 和 kivy
- Business collocations
- Super Learning Method
猜你喜欢
随机推荐
用匿名函数定义函数_c语言最先执行的函数是
JS工厂模式_工厂模式进行封装
【励志】复盘的重要性
zabbix deployment
AWS Lambda相关概念与实现思路
MySQL: Integrity Constraints and Table Design Principles
Jenkins User Manual (1) - Software Installation
ROI LTV CPA ECPM体系讲解
【Idea系列】idea配置
Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
The difference between Mysql application log time and system time is eight hours
STM32前言知识总结
zabbix部署
MySQL:完整性约束和 表的设计原则
一文带你了解 ESLint
nsq部署_andlua辅助源码
MySQL之my.cnf配置文件
HTB-Sense
Apache Calcite 框架原理入门和生产应用
What is the principle of thermal imaging temperature measurement?Do you know?









