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

ROI LTV CPA ECPM体系讲解

Maple 2022 software installation package download and installation tutorial

Digital management insight into retail and e-commerce operations - retail password

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

图文手把手教程--ESP32 MQTT对接EMQX本地服务器(VSCODE+ESP-IDF)
![[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss](/img/4d/9c2f94f475834771f6ad6ffe8f8b35.png)
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss

iMeta | 百度认证完成,搜索“iMeta”直达出版社主页和投稿链接

Camunda overall architecture and related concepts

Small program containers accelerate the construction of an integrated online government service platform

iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
随机推荐
OD-Model【5】:YOLOv1
sqlilabs less-38~39
Super Learning Method
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
2万字50张图玩转Flink面试体系
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
Multimedia and Internet of Things technology make the version "live" 129 vinyl records "Centennial Voice"
Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
常用的输入对象
Maple 2022 software installation package download and installation tutorial
Jina 实例秀|七夕神器!比你更懂你女友的AI口红推荐
Small program containers accelerate the construction of an integrated online government service platform
第二批养老理财试点产品发行 一小时销售20亿元
什么是终端特权管理
图文手把手教程--ESP32 MQTT对接EMQX本地服务器(VSCODE+ESP-IDF)
Heap Sort
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
黑马瑞吉外卖之员工账号的禁用和启用以及编辑修改
为企业数字化转型提供服务_数字赋能企业转型
无代码平台描述文字入门教程