【c++系列】map容器实现的word count

说明

使用map容器实现word count。


代码

#include <iostream>
#include <map>


using namespace std;


int main(int argc,char **argv){
	string slist[]={"c++","java","python","c++","python","c++"};


	size_t slits_size = sizeof(slist)/sizeof(*slist);
	
	string *begin=slist,*end=slist+slits_size;


	map<string,int> word_count;
	
	while(begin!=end){
		++word_count[*begin++];
	}


	map<string,int>::const_iterator iter=word_count.begin();
	while(iter!=word_count.end()){
		cout<<iter->first<<" : "<<iter->second<<endl;
		iter++;
	}
}



你可能感兴趣的:(C++,map)