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; }
查看全文
相关阅读:
鸟哥的Linux私房菜基础学习篇(第三版)——阅读笔记(二)
鸟哥的Linux私房菜基础学习篇(第三版)——阅读笔记(一)
Git的使用流程及常用命令汇总
Protocol buffers编写风格指南
Effective Go中文版(更新中)
go package 学习笔记 —— strconv(string与其他基本数据类型(int, float, bool)的转换)
在iis上部署 AngularJS
asp.net Core 2.0部署到iis上
ABP 运行前端代码
Asp.NET Core2.0 EF ABP Postgresql 数据迁移
原文地址:https://www.cnblogs.com/dyufei/p/2573900.html
最新文章
iOS 自定义viewController 切换 (modal)
编程是一种乐趣
原生js-拉勾网首页效果
js 获取元素在页面上的偏移量的最佳方式
css ie hack整理
javascript中的'this'
gulp完成javascript压缩合并,css压缩
Nginx安装负载均衡配置 fair check扩展
C# 通过copydata实现进程间通信
Vue专题
热门文章
Linux相关
闭包
JS中的数组
JS中常见的兼容
HTML基础1
JS中的循环
CSS3中的弹性盒子模型
一个有关原码、反码、补码的小问题
Golang package轻量级KV数据缓存——go-cache源码分析
etcdctl的使用
Copyright © 2011-2022 走看看