1. 程式人生 > >⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄. 菜鳥不吃草!

⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄. 菜鳥不吃草!

切分字串,統計詞頻。

//6-21
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 100;

void split(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
  std::string
::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while(std::string::npos != pos2) { v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if(pos1 != s.length()) v.push_back(s.substr(pos1)); } int main(){ string str; vector<string
>
vec; vector<string> key; getline(cin,str); split(str,key," "); while(getline(cin,str)){ split(str,vec," "); } vector<string>::iterator it; sort(vec.begin(),vec.end()); /* for(it = vec.begin();it != vec.end();it ++){ cout<< *it <<endl; }*/
for(int i=0;i<key.size();i++){ int counts = count(vec.begin(),vec.end(),key[i]); cout<< key[i] << ": " << counts <<endl; } return 0; } /* this is a book It is a cat this book This is book that is a tree */