前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++统计字符频数_英文字符

C++统计字符频数_英文字符

作者头像
yifei_
发布2022-11-14 14:18:12
3720
发布2022-11-14 14:18:12
举报
文章被收录于专栏:yifei的专栏

之前有一个需求:统计一个混合中英文字符和各种符号的文件的各字符数量,结果发现,事情并没有想象的那么简单.于是先写个只统计英文字符的程序吧.

代码语言:javascript
复制
//data.txt需要放在相同目录下
//只能计算纯英文文件中的字符.
#include <iostream>
#include <map>
#include <fstream>
#include <algorithm>
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;

int main(){
	ifstream infile("data.txt");
	string s1;
	char c;
	map<char,int> m1;
	multimap<double,char> m2;
	int sumchar=0;
	
	clock_t start,end;

	start=clock();
	while(infile.get(c)){
		m1[c]++;
		sumchar++;
	}
	
	std::map<char,int>::iterator it1=m1.begin();
	for(;it1 != m1.end(); it1++){
		m2.insert(pair<double,char>((it1->second*1.0/sumchar),it1->first));
	}
	
	end=clock();

	cout<<"共计:"<<(end-start)/1000<<"毫秒"<<endl;
	//cout<<(end-start)/CLOCKS_PER_SEC<<"秒"<<endl;
	cout<<"总计:"<<sumchar<<"个字符"<<endl<<endl;
	
	
	std::multimap<double,char>::iterator it2=m2.begin();
	for(;it2 != m2.end();it2++){
		cout<<it2->second<<" "
			<<setw(4)<<it2->first*100<<"%"
			<<endl;
	}
}

知识点

欢迎与我分享你的看法。 转载请注明出处:http://taowusheng.cn/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 知识点
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档