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; }
查看全文
相关阅读:
《Python核心编程》第二版第209页第八章练习 续二 Python核心编程答案自己做的
第三章.md
沙箱逃逸.md
setjmp.md
HTML5 and JavaScript Projects
递归裴波那契数列
Smashing HTML5
Sams.Teach.Yourself.CPP.in.One.Hour.a.Day.7th.Edition
c++ 静态成员
Getting Started with Metro Style Apps
原文地址:https://www.cnblogs.com/dyufei/p/2573900.html
最新文章
Json在线编辑和验证工具
C#常用正则表达式
js编辑器
ico生成工具
javascript 开发利器
Silverlight查看CPU使用情况
Silverlight跨线程问题解决办法
silverlight base64编码 Image
python实现linux kill进程的小程序
eclipse中安装Pydev编辑器
热门文章
nodejs 与 V8 点滴
《Python核心编程》第二版第160页第六章练习 续五 Python核心编程答案自己做的
Python的绿色安装
《Python核心编程》第二版第186页第七章练习
《Python核心编程》第二版第162页第六章练习 续七
《Python核心编程》第二版第209页第八章练习 续一 Python核心编程答案自己做的
怎样在Win7 64位旗舰版安装Python+Eclipse开发环境
《Python核心编程》第二版第162页第六章练习 续六 Python核心编程答案自己做的
《Python核心编程》第二版第186页第七章练习 续一
《Python核心编程》第二版第209页第八章练习 Python核心编程答案自己做的
Copyright © 2011-2022 走看看