Set Map pair

Set

Set<int> s;  //同vector用法, 插入为insert

Set是有序不重复的容器

Map

Map<int,int> mp;  //较多

构造函数、插入等同vector

c++98中

Map<string,int>Literator it;

for(it=m2.begin(),it!=m2.end();it++)

{

  string s=it->first;

  printf("%s %d",s.data(),it->second);

}

注: it->first 代表map的键值

   it->second代表map的值

C++11

for(auto it:map)

  cout<<it.first<<" "<<it.second<<endl;

Pair

头文件 #include <utility>

pair<string,int> p; 作用类似于map

map不能按照值来排序,为此须要借用vector和pair来进行排序

map<string,map> mp;
vector<pair<string,map>> v(mp.begin(),mp.end());
sort(v.begin(),v.end(),cmp);


bool cmp(pair<string,int> a,pair<string,int> b)
    retrun a.second>b.second;

参考PAT乙级1085

posted @ 2022-05-29 12:11  ChioHiro  阅读(76)  评论(0)    收藏  举报