会员
周边
新闻
博问
融资
云市场
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
DYF
我思故我在!
博客园
首页
新随笔
新文章
联系
订阅
管理
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; }
posted on
2012-01-06 00:07
o(∩_∩)o...
阅读(
3200
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部