c++中STL容器中的排序

1.c++STL中只有list自带了排序函数:

(1).若list中存放的是int类型或者string类型,直接利用sort即可:

list <int> list1;

          list1.sort();
         此时默认为升序,若要使用降序,直接自定义cmp函数即可。
(2).若存放的是结构体或其他指针类型,需要自己定义比较结构体:
          struct student
{
int num;
};
          struct node
{
bool operator()(student a,student b)

return a.num>b.num;//从大到小排序
};

list<student> list1;
list1.sort(node());
 
 
2.vector排序:
使用algorithm中的sort函数:

#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int cmp(int a,int b){

return a > b;//按降序排序

}

int main(){

vector<int> vec;

vec.push_back(324);

vec.push_back(344);

vec.push_back(2134);

vec.push_back(23);

vec.push_back(12);

vec.push_back(134);

vec.push_back(987);

sort(vec.begin(),vec.end(),cmp);

vector<int>::iterator it;

for(it = vec.begin();it != vec.end();it++)

cout<<*it<<" ";

cout<<endl;

return 0;

}

3.STL中的set排序:其中set本身是自带排序的,默认按升序进行排序:

#include <iostream>
#include <set>
using namespace std;
int main()
{
    set <int,less<int> > set1;//若使用less,则从小到大,若使用greater则从大到小。
    set1.insert(20);
    set1.insert(15);
    set1.insert(0);
    set1.insert(80);
    set<int>::iterator cset=set1.begin();
    while(cset!=set1.end())
    {
        cout<<*cset<<" ";
        cset++;
    }
    int m;
    cin>>m;
    return 0;
}
 
 

若定义set中存储的为结构体:

#include <iostream>

#include <string>

#include <set>

using namespace std;

struct intComp {

bool operator() (const int& lhs, const int& rhs) const{

return lhs > rhs;

}

};

struct strComp

{

bool operator() (const string& str1, const string& str2) const {

return str1.length() < str2.length();

}

};

int main() {

int a[] = {10, 20, 30, 40, 50};

set<int, intComp> s1(a, a + 5);

for (auto it = s1.cbegin(); it != s1.cend(); it++)

{

cout << *it << " ";

}

cout << endl;

string b[] = {"apple", "banana", "pear", "orange", "strawberry"};

set<string, strComp > s2(b, b + 5);

for (auto it = s2.cbegin(); it != s2.cend(); it++)

{

cout << *it << " ";

}

cout << endl;

system("pause");

return 0;

}

 
4.STL中map:
由于在map中是由红黑树实现的所以自带生序排序(key)
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map <int,string,less<int> > map1;//使用less有小到大 greater由大到小
map1[1]="HZ";
map1[8]="yu";
map1[100]="rue";
map1[-10]="23";
map<int,string>::iterator cmap=map1.begin();
while(cmap!=map1.end())
{
    cout<<cmap->first<<" ";
    cmap++;
}
int m;
cin>>m;
    return 0;
}
若存储的是结构体:
#include <iostream>
#include <map>
using namespace std;
struct student
{
int num;
};
bool operator <(student a,student b)
{
    return a.num>b.num;//由大到小
}
int main()
{
    map<student,int> map1;
    student a;
    a.num=100;
    map1[a]=10;
   
    student b;
    b.num=1000;
    map1[b]=11;
    student c;
    c.num=0;
    map1[c]=102;
    map<student,int> ::iterator st=map1.begin();
    while(st!=map1.end())
    {
        cout<<(st->first).num<<" ";
        st++;
    }
    int m;
    cin>>m;
    return 0;
}
map:(value)
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
struct CmpByValue { 
  bool operator()(const pair<string,int> & lhs, const pair<string,int> & rhs)
  {return lhs.second > rhs.second;} 
};
void substrcount(string str)
{
    map<string,int> substr;
    string subs;
    for(int i =1; i<str.size();i++)
        for(int j =0; j<=str.size()-i;j++)
        {
subs = str.substr(j,i);
            if(substr.find(subs)==substr.end())
                substr[subs] = 1;
            else
                substr[subs] = substr[subs]+1;
        }
    vector<pair<string,int>> counts(substr.begin(),substr.end());
    sort(counts.begin(),counts.end(),CmpByValue());
    cout<<(counts.begin()->first)<<endl;
    cout<<counts.begin()->second<<endl;
}
 
上一篇:【pat】C++之刷题常用STL容器整理


下一篇:转载《五大免费采集器哪个好,火车头,海纳,ET,三人行,狂人采集 》