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;
}
 

c++中STL容器中的排序的更多相关文章

  1. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  2. C++中STL容器的比较

    基本参考 https://blog.csdn.net/qq_14898543/article/details/51381642 容器特性: vector:典型的序列容器,C++标准严格要求次容器的实现 ...

  3. 删除STL容器中的元素

    有关stl容器删除元素的问题,错误的代码如下: std::vector<struct> mFriendList; ... std::vector<struct>::iterat ...

  4. STL容器——对map排序

    STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...

  5. WPF中Grid容器中VerticalAlignment和HorizonAlignment和Margin的关系。

    在WPF中,经常使用Grid容器,来布局我们想要显示的对象. 这就不可避免的要和布局在其中的控件的VerticalAlignment特性,HorizonAlignment特性,以及Magin特性打交道 ...

  6. c++中stl容器的常用示例

    1. set(集合)——包含了经过排序了的数据,这些数据的值(value)必须是唯一的. 也就是说输入set容器后得到数据,会去重并排序.    s.insert()插入一个元素    s.begin ...

  7. 自定义String类,并且实现在STL容器中添加自定义的类型

    13.44 编写标准库string类的简化版本,命名String.你的类应该至少有一个默认构造函数和一个接受C风格字符串指针参数的构造函数.使用allocator为你的String类分配所需内存. 1 ...

  8. spring中获取容器中的Bean为什么前转成接口而不是实现类

    简单介绍一下上下文,userService是服务层接口有一个save方法,userServiceImpl是该接口的实现类重写了save方法. applicationContext.xml如图: 后台代 ...

  9. ios 中在容器中移除单个控件的两个方法Subview

    我们知道[parentView removeFromSuperView];  会把全部的view都移除.以下我们可以通过给subView设一个tag,然后遍历所有的subView,找到目标subVie ...

随机推荐

  1. csv文件读写处理

    csv文件读写处理   1.读取 第一种:   import csv with open("route.csv","r") as f: #reader是一个迭代 ...

  2. html中form表单的使用方法和介绍

    from表单的使用方法 一.表单赏析 二.了解表单功能:用于搜集不同类型的用户输入的内容 有了表单,网页的内容可以由用户自己创建,那么对于网页来说,我们既是网页创建都者,也是网页的消费者. 三.常用的 ...

  3. 二、linux的安装

    1. 虚拟机安装: 1.1. 什么是虚拟机 虚拟机:一台虚拟的电脑. 虚拟机软件: * VmWare :收费的. * VirtualBox :免费的. 1.2. 安装VmWare 参考<虚拟软件 ...

  4. 通读SCRUM实战指南教材,提出5个问题。

    问题一:为什么要制定优先级的排定和调整? 创建一个排好优先级的项目组合并讲重点放在转移团队上,如果做得对,可以消除在项目数量超过团队能力 的情况下过于常见的多任务处理. 问题二:为什么我们要做文档? ...

  5. 洛谷P1993 小K的农场

    思路是差分约束+dfs版SPFA. 首先来思考差分约束的过程,将题目给出的式子进行转化: 农场a比农场b至少多种植了c个单位的作物, SPFA我们考虑跑最短路,那么要让SPFA中满足的式子就是if(d ...

  6. FNMP

    Table of Contents 平台 FNMP安装 FNMP配置 php配置 mysql配置 nginx配置 phpMyAdmin配置 平台 freeBSD 12.0 FNMP安装 php安装 v ...

  7. lintcode中等题目的四道题

    第一题: 第一句先创建了一个解决问题的类, 第二句声明了一个公共接口的整形链表里面有N个整形数, 第三句给链表动态初始化, 第四步判断链表里的数有没有,如果N<=0则返回结果0,否则执行Prin ...

  8. LeetCode算法历程-02

    编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true ...

  9. 前端校验框架ValidForm之check方法修正

    用过validform的朋友相信都知道,在利用check方法的时候,发现该方法对表单输入值只要符合datatype规则的就返回ture.那么我们想对某个字段进行ajax重复校验的时候,只需要在该表单元 ...

  10. 北大poj- 1013

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50515   Accepted: 15 ...