map 基础使用----实现一个统计字符数量的算法
#include <map>
#include <algorithm>
关键字:map、inster、earse、pair、iterator、const_iterator
template<class T>
void printMap_Vector(T & tmp_con) //模板函数,输出map与保存map元素迭代器的vector
/************************************************************************/ /* map是STL的关联容器 key value 键值对 key不可重复 内部为红黑树 自动排序 #include <map> #include <algorithm>//容器算法的头文件(inster、begin、end、clear、erase) */ /************************************************************************/ #include <iostream> #include <map> #include <vector> #include <string> #include <algorithm> using namespace std; bool cmp_by_value(const pair<int,string>& v1,const pair<int,string>& v2){ return v1.second <v2.second; } struct CmpByValue { bool operator()(const pair<int,string>& v1,const pair<int,string>& v2){ return v1.second <v2.second; } }; typedef map<int,string> My_Map; //定义一个map类型别名 My_Map map_tets; void printMap(My_Map &tmp_map){ My_Map::iterator it = tmp_map.begin(); for (;it != map_tets.end();it++) { cout<<"Key: "<<it->first<<" value: "<<it->second<<endl; } } void printVector(vector<pair<int,string>> &tmp_vector){ vector<pair<int,string>>::iterator it = tmp_vector.begin(); for (;it != tmp_vector.end();it++) { cout<<"Key: "<<it->first<<" value: "<<it->second<<endl; } } //模板函数 template <class T> void printMap_Vector(T &tmp_con){ T::iterator it = tmp_con.begin(); for (;it != tmp_con.end();it++) { cout<<"Key: "<<it->first<<" value: "<<it->second<<endl; } } void statistic_fuction(char str[],map<char,int> &tmp_vector){ char *p; p = str; map<char,int>::iterator it; while (*p !='\0') { it = tmp_vector.find(*p); if (it != tmp_vector.end()) { tmp_vector[*p]++; }else { tmp_vector[*p] = 1; } p++; } } void printVector(const map<char,int> &tmp_vector){ map<char,int>::const_iterator it = tmp_vector.begin(); for (;it != tmp_vector.end();it++) { //cout<<"key :"<<(it->first)<<" cnt:"<<it->second<<endl;//打印方式一 printf("key:%c cnt:%d \n",it->first,it->second); //打印方式二 %c } } int main(){ map_tets[2] = "李四"; map_tets.insert(make_pair(4,"赵六")); map_tets.insert(pair<int,string>(1,"张三")); map_tets.insert(map<int,string>::value_type(3,"王五")); vector<pair<int,string>> tmp_vector(map_tets.begin(),map_tets.end()); //sort(tmp_vector.begin(),tmp_vector.end(),cmp_by_value); //排序方式一 sort(tmp_vector.begin(),tmp_vector.end(),CmpByValue()); //排序方式二 重载() cout<<"--------------map_tets-----------------"<<endl;//默认key排序, //printMap(map_tets);//map 输出 printMap_Vector(map_tets);//使用自定义map vector 模板函数输出 cout<<"--------------sort tmp_vector-----------------"<<endl;//转换为vector后使用value排序 //printVector(tmp_vector);//vector 输出 printMap_Vector(tmp_vector);//使用自定义map vector 模板函数输出 cout<<"-------------------------------"<<endl; map_tets.erase(map_tets.begin()); //删除首位元素 map_tets.erase(map_tets.find(2)); //删除指定位置元素 //printMap(map_tets); //map 输出 printMap_Vector(map_tets);//使用自定义map vector 模板函数输出 //输入一串字符,统计每个字符出现的频率,使用map保存结果 char str[256]; cin>>str; map<char,int> statistic_vector; statistic_fuction(str,statistic_vector); //printVector(statistic_vector);//vector 输出 printMap_Vector(statistic_vector);//使用自定义map vector 模板函数输出 system("pause"); return 0; }
留言评论
暂无留言