zoukankan
html css js c++ java
c++ 从文件中读取字符串 按字典排序 并统计个数
#include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator> #include <map> #include <fstream> using namespace std; const int MAX_SIZE = 40; void GetWords(vector<string> &vec) { char str[ MAX_SIZE]; ifstream InFile("E:\\test.txt"); while(!InFile.eof()) { InFile.getline(str,MAX_SIZE); const char * split = ",* ;"; char * p; p = strtok (str,split); while(p!=NULL) { string str = p; vec.push_back(str); p = strtok(NULL,split); } } copy (vec.begin(),vec.end(),ostream_iterator<string>(cout," ")); } void SortWords( vector<string> &vec,map<string,int> &words) { vector<string>::iterator it = vec.begin(); for(it; it != vec.end(); ++it) { if(words.find(*it) != words.end() ) { int i = words[*it]; ++i; words[*it] = i; } else { words.insert(map<string,int>::value_type(*it,1)); } } } int _tmain(int argc, _TCHAR* argv[]) { vector<string> vec; map<string,int> words; GetWords(vec); SortWords( vec,words); cout<<endl<<"====After sort===="<<endl; map<string,int>::iterator mapit = words.begin(); for(mapit;mapit != words.end();++mapit) { cout<< mapit->first<<" "<<mapit->second<<endl; } return 0; }
查看全文
相关阅读:
微信支付
微信分享
微信获取用户
js处理富文本编辑器转义、去除转义、去除HTML标签
最新亲测能用的手机号码正则
JS监听页面滚动到底部事件
解决windows系统80端口被占用问题
thinkphp5.0配置nginx重写规则
php关于private、protected、public的区别
centeros7远程访问mysql5.7
原文地址:https://www.cnblogs.com/dyufei/p/2573900.html
最新文章
C++之纯虚函数
C++之类成员所占内存大小问题总结
Makefile中的“-I”(大写i),“-L”(大写l),“-l”(小写l)
Java 设计模式
Java 集合
Java Socket
Windows 常见DOS命令
Tomcat常见问题
JVM 性能调优监控工具
SoapUI性能测试
热门文章
Java 反射
Java中的String、StringBuffer和StringBuilder的区别
Java Compare接口
反向代理(端口映射)
curl
centos7.4安装nginx1.8.1 php7.7.11 安装 MySQL5.7.20
crontab(定时任务)
redis安装和外部链接
bcmath(精准数学的计算)
登陆安全验证token(利用redis缓存和http请求头做的登陆验证)
Copyright © 2011-2022 走看看