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

C++实现文件中单词统计等

采用C++的容器、迭代器、泛型算法等实现:从指定文本文件(英文文章)中读取单词,过滤掉指定过滤词,并统计剩下的单词出现次数。

使用了vector,string,set,map,istream_iterator,ostream_iterator, copy等STL元素。

//copy_clean_filter.cpp
//
//读取指定文本(英文)文件的内容,
//1. 输出其中不含标点和排除单词的所有单词
//2. 统计每个单词出现次数
//3. 按照次数排序依次输出不同频次对应的单词列表#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <cctype>
#include <unordered_set>
#include <map>
#include <numeric>
#include <windows.h>
using std::string;
using std::map;
using std::vector;
//using std::ordered_map;
// 1. 移除字符串中的标点符号
std::string remove_punctuation(const std::string& s) {std::string result;std::copy_if(s.begin(), s.end(), std::back_inserter(result),[](unsigned char c) { return !std::ispunct(c)&&!std::isdigit(c); });return result;
}// 2. 转换为小写(确保排除词匹配不区分大小写)
std::string to_lower(const std::string& s) {std::string result;std::transform(s.begin(), s.end(), std::back_inserter(result),[](unsigned char c) { return std::tolower(c); });return result;
}int main() {const std::unordered_set<std::string> exclude = {"a", "an", "the", "and", "but", "so", "or"," ","for","in","is","at","of","to","it","if","as"};//SetConsoleOutputCP(65001); // 设置控制台输出为 UTF-8//SetConsoleCP(65001);       // 设置控制台输入为 UTF-8std::string filename;map<string, int> word_cnt;std::cout << "请输入文件名: ";std::cin >> filename;std::ifstream file(filename);if (!file) {std::cerr << "无法打开文件: " << filename << std::endl;return -1;}// 读取原始单词到临时容器std::vector<std::string> raw_words;std::copy(std::istream_iterator<std::string>(file),std::istream_iterator<std::string>(),std::back_inserter(raw_words));// 1. 处理:去标点 + 转小写std::vector<std::string> processed_words;std::transform(raw_words.begin(), raw_words.end(),std::back_inserter(processed_words),[](const std::string& s) {return to_lower(remove_punctuation(s));});// 2. 过滤:排除空字符串和禁用词std::vector<std::string> filtered_words;std::remove_copy_if(processed_words.begin(), processed_words.end(),std::back_inserter(filtered_words),[&exclude](const std::string& s) {return s.empty() || exclude.count(s);});// 输出结果std::cout << "\n处理后的单词列表:\n";for (const auto& w : filtered_words) {std::cout << w << " ";}// 统计每个单词出现次数for (auto& s : filtered_words)word_cnt[s]++;for (auto& s : word_cnt)std::cout << s.first << ":" << s.second << std::endl;std::cout << std::endl;//按 频次:对应单词 造表map<int, vector<string>>  freq_words;for (auto& s : word_cnt)freq_words[s.second].push_back(s.first);std::cout << string("\n频次及对应单词列表") << std::endl;for (auto& s : freq_words){std::cout << s.first << "("<<s.second.size()<<")" << string(":");std::copy(s.second.begin(), s.second.end(),std::ostream_iterator<string>(std::cout, " "));std::cout << std::endl;}// 使用 std::for_each 替代 for 循环std::cout << string("\n使用 std::for_each 替代 for 循环") << std::endl;std::for_each(freq_words.begin(),freq_words.end(),[](const auto& s) {  // Lambda 表达式处理每个键值对std::cout << s.first << "(" << s.second.size() << "): ";// 输出 vector 中的所有字符串std::copy(s.second.begin(),s.second.end(),std::ostream_iterator<std::string>(std::cout, " "));std::cout << std::endl;});//测试数值算法--与本例无关std::vector<int> vec(5);/*用一个起始值依次递增地为范围内的元素赋值,第一个参数是起始迭代器,第二个参数是结束迭代器,第三个参数是初始值,每次赋值后该值会自动加 1*/std::iota(vec.begin(), vec.end(), 0);std::copy(vec.begin(),vec.end(),std::ostream_iterator<int>(std::cout, " "));std::cout << std::endl;return 0;
}


文章转载自:

http://miAvyT5o.Lptjt.cn
http://TlDIK1Wv.Lptjt.cn
http://zHgPOyaz.Lptjt.cn
http://0WLizGvu.Lptjt.cn
http://De2GXRve.Lptjt.cn
http://YoOsl2cs.Lptjt.cn
http://hoXtCjDj.Lptjt.cn
http://qiWjl2cK.Lptjt.cn
http://nwefiyFz.Lptjt.cn
http://KxTiSBJp.Lptjt.cn
http://U26wOFr1.Lptjt.cn
http://AIbabyMm.Lptjt.cn
http://UPjm5XXE.Lptjt.cn
http://gi2pA4Zf.Lptjt.cn
http://hxmf9DOm.Lptjt.cn
http://4rziUVJZ.Lptjt.cn
http://syNQkYmd.Lptjt.cn
http://wteDfM5M.Lptjt.cn
http://DjQ4NSzg.Lptjt.cn
http://RrxedOPf.Lptjt.cn
http://1GkBK1Ae.Lptjt.cn
http://YPpZ04Ty.Lptjt.cn
http://AhNT5xPZ.Lptjt.cn
http://Qd9eB607.Lptjt.cn
http://LiIanbNk.Lptjt.cn
http://MA5NcRYE.Lptjt.cn
http://gHTr7HXz.Lptjt.cn
http://MdMz3kA4.Lptjt.cn
http://p19Mf0QE.Lptjt.cn
http://ni6nou75.Lptjt.cn
http://www.dtcms.com/a/385525.html

相关文章:

  • 数据库(四)MySQL读写分离原理和实现
  • 关于数据库的导入和导出
  • 【氮化镓】GaN中受主的氢相关钝化余激活
  • AI 进课堂 - 语文教学流程重塑
  • 最近一些机器github解析到本地回环地址127.0.0.1
  • P6352 [COCI 2007/2008 #3] CETIRI
  • 【LeetCode 每日一题】37. 解数独
  • 多项式回归:线性回归的扩展
  • AI生成到无缝PBR材质:Firefly+第三方AI+Substance工作流
  • Java分布式锁实战指南:从理论到实践
  • 【CSS】层叠上下文和z-index
  • inline-block元素错位原因及解决方法
  • 【Java】P3 Java基础:关键字、标识符与变量详解
  • Golang语言入门篇003_Go源代码结构
  • 【Docker】报错Data page checksums are disabled.
  • Viper:Go语言中强大的配置管理库入门教程
  • ISO/PAS 5112 附录A 与21434 WPs的映射关系
  • 机器学习-Bagging
  • OpenCV 图像拼接实战:从特征检测到全景融合
  • Atlas-Chain:一个灵活的Java责任链框架设计与实现
  • FBX/OBJ/MAX/GLB/GLTF怎么处理成3dtiles,制作B3DM格式模型文件
  • 金融数据---获取问财数据
  • Python(1)|| 超基础语法(格式,输入输出,变量,字符串,运算符)
  • Linux 文本处理三剑客:grep、sed 与 awk
  • docker-webtop+cpolar:无感远程Linux桌面方案
  • 随机森林模型:基于天气数据集的分类任务全流程解析
  • Linux vim快捷键记录
  • 聊聊大模型的self-training:从Test-time RL说起
  • 星穹无损合约:以信任为基石,开启DeFi新纪元
  • cJSON的安装和使用