当前位置: 首页 > news >正文

单词排序C++实现

在这里插入图片描述
代码如下:

#include<iostream>
#include<string>
#include<fstream>
#include<map>
#include<iomanip>
#include<algorithm>
#include<vector>int read_file(std::map<std::string,int> &map_words)
{std::string filename = "words.txt";std::fstream fin;fin.open(filename.c_str(), std::ios::in);std::string tmp;while (fin >> tmp){++map_words[tmp];}return 0;
}int save_words(std::multimap<int, std::string, std::less<int>> &multimap_words, std::map<std::string,int> map_words)
{for(std::map<std::string, int>::iterator iter = map_words.begin(); iter != map_words.end(); ++iter)
{multimap_words.insert(std::pair<int, std::string>(iter->second, iter->first));
}return 0;
}int print_words(std::multimap<int, std::string, std::less<int>> multimap_words)
{int i = 0;for(std::multimap<int, std::string>::iterator iter = multimap_words.begin(); (iter != multimap_words.end()); ++iter, ++i){if((iter->first == prev(iter,1)->first) || (i < 20)  || 1){std::cout << "第" << i << "个单词,它出现的频次是:" << iter->first << " 它是:" << iter->second << std::endl;}else{break;}}return 0;
}int main(int argc, char* argv[])
{std::map<std::string,int> map_words;read_file(map_words);std::multimap<int, std::string, std::less<int>> multimap_words;save_words(multimap_words,map_words);print_words(multimap_words);return 0;
}

运行程序的结果如下:
image.png
image.png


http://www.mrgr.cn/news/23868.html

相关文章:

  • Spring Boot 注解探秘:JSON 处理的魔法世界
  • 1.滑动窗口问题
  • 冒泡排序;选择排序;插入排序;快排;判断大小端;位运算
  • Delphi 12.1安卓APP开发中获取硬件信息及手机号
  • JAVA进阶学习15
  • 【Go】-Gin框架
  • LCR 021
  • 智能优化算法-樽海鞘优化算法(SSA)(附源码)
  • 一款好用的电子样本册转换器
  • JAVA中在junit测试类中加载Spring托管,通过注解使用Bean
  • shell的使用sed处理数据
  • 轻量级模型解读——GhostNet系列
  • Git版本控制工具
  • day-52 三数之和
  • Qt放Element网页滑动菜单栏
  • udp的广播,多播,单播 demo
  • 图书馆上新了!新华书店×度小满推出“开学季悦读计划”公益活动
  • OpenCV findTours函数及其用法
  • Hive SQL子查询应用
  • springboot基础-Druid数据库连接池使用