一、排序 sort

sort(first_pointer,first_pointer+n,cmp)
默认为升序

若要使用降序,自行写cmp 函数

bool cmp(int a,int b)
{
return a<b; //升序排列,如果改为return a>b,则为降序
}

例如:
方法一:定义比较函数(最常用)
//情况一:数组排列
int A[100];
bool cmp1(int a,int b)//int为数组数据类型
{
return a>b;//降序排列
//return a<b;//默认的升序排列
}
sort(A,A+100,cmp1);

//情况二:结构体排序
Student Stu[100];
bool cmp2(Student a,Student b)
{
return a.id>b.id;//按照学号降序排列
//return a.id<b.id;//按照学号升序排列
}
sort(Stu,Stu+100,cmp2);
注:比较方法也可以放在结构体中或类中定义。

//实现对map按value进行排序
map中含两个值分别为key和value,map是按照key值进行排序的,若value值进行排序,如下:
typedef pair<string, int> PAIR;
int cmp(const PAIR & x, const PAIR & y)
{
return x.second > y.second;
}

map<string,int> m;
vector<PAIR> vec;
for (map<wstring,int>::iterator curr = m.begin(); curr != m.end(); ++curr)
{
vec.push_back(make_pair(curr->first, curr->second));
}
sort(vec.begin(), vec.end(), cmp);
将map的key和value组成一个新的结构PAIR,一个PAIR型的vector存储map中的所有内容,对vecor按照value值进行排序。按顺序输出key:

注意:如果是类的话,这个cmp的定义要放在类的外面,作为非成员函数 或者定义成 static 成员函数(推荐),不然编译会出错(如图)。

正确写法:

将cmp定义为类的static 成员函数

class Solution {
public:
    static  bool compare(vector<int> a, vector<int> b)    //将compare定义成 static  //不然会报如上错误
    {
        if (a[0] == b[0]) {
            return a[1] < b[1];
        } else {
            return a[0] > b[0];
        }
    }
    vector<vector<int>> ReconstructQueue(vector<vector<int>>& people)
    {
        sort(people.begin(), people.end(), compare);
        vector<vector<int>> res;
        for (int i = 0; i < people.size(); i++) {   
            res.insert(res.begin() + people[i][1], people[i]);
        }
        return res;
    }
};
 
二、 next_permutation  全排列

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
string s;
getline(cin,s);
sort(s.begin(),s.end());
do
{
  cout<<s<<endl;
  }while(next_permutation(s.begin(),s.end()));
  return 0;
}

注: next_permutation 原型:
#include <algorithm>
bool next_permutation(iterator start,iterator end)

注意:使用之前需要对所求全排列的数组进行升序排序。(因为它是基于当前数组去求下一个全排列)

STL中常用算法的更多相关文章

  1. STL中的算法

    STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baidu.com/ding ...

  2. 仿函数(二、stl中常用仿函数)

    提到C++ STL,首先被人想到的是它的三大组件:Containers, Iterators, Algorithms,即容器,迭代器和算法.容器为用户提供了常用的数据结构,算法大多是独立于容器的常用的 ...

  3. STL中的算法小结

    ()要运用STL的算法,首先必须包含头文件<algorithm>,某些STL算法用于数值处理,因此被定义于头文件<numeric> ()所有STL算法都被设计用来处理一个或多个 ...

  4. STL中copy算法

    STL中通过使用copy函数以提供一种方便的方式来输出容器中的元素.函数copy作为泛型算法的一部分,任何容器类型都可以使用.由于我们需要频繁的初始容器的元素,因此在继续讨论容器之前,先学习一下cop ...

  5. STL中常用容器及操作 学习笔记1

    @[TOC](下面介绍STL中常见的容器及操作)## 不定长数组 vector> vetcor:其实就是一个数组或者说是容器 其操作不同于之前直接定义的数组 > 而且可以直接赋值也可以直接 ...

  6. stl中常用的排序算法

    #include"iostream" #include"vector" using namespace std; #include"string&qu ...

  7. STL中常用的c++语法

    函数调用操作(c++语法中的左右小括号)可以被重载,STL的特殊版本都以仿函数形式呈现.如果对某个class进行operator()重载,它就成为一个仿函数. 仿函数(functor),就是使一个类的 ...

  8. C++ STL 之 常用算法

    #include <iostream> #include <vector> #include <algorithm> using namespace std; // ...

  9. STL中heap算法(堆算法)

     ①push_heap算法 以下是push_heap算法的实现细节.该函数接收两个迭代器,用来表现一个heap底部容器(vector)的头尾,而且新元素已经插入究竟部的最尾端. template ...

随机推荐

  1. 复变函数-MINDMAPS-continuous updating

  2. phantomJS安装出错解决办法

    解决办法:https://github.com/xhlwill/blog/issues/11

  3. P3366【模板】最小生成树

    P3366[模板]最小生成树 Kruskal #include <bits/stdc++.h> using namespace std; typedef long long ll; ; ; ...

  4. javaWeb普通类获取ApplicationContext

    网上看了很多关于获取ApplicationContext的方法,五大方法,但是我用web服务使用成功的就这一个,自己记忆下. import javax.servlet.ServletContextEv ...

  5. asp.net MVC3.0 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction

    asp.net MVC3.0 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction 1.带有Render的方法返回值是v ...

  6. #!/usr/bin/python

    它是用来指定用什么解释器运行脚本以及解释器所在的位置. 参考链接:https://www.cnblogs.com/qmfsun/p/6291982.html

  7. 阿里短信回持.net sdk的bug导致生产服务cpu 100%排查

    一:背景 1. 讲故事 去年阿里聚石塔上的所有isv短信通道全部对接阿里通信,我们就做了对接改造,使用阿里提供的.net sdk. 网址:https://help.aliyun.com/documen ...

  8. Asp.net core logging 日志

    1 基本概念 Dotnet core 一个重要的特征是 Dependency injection ,中文一般是依赖注入,可以简单理解为一个集合,在应用程序启动时,定义各种具体的实现类型并将其放到集合中 ...

  9. 14.Java连接Redis_Jedis_主从模式

    redis的主从模式之前提到过,这里我们使用redis来实现主从模式. 首先在VMware虚拟机中的Linux中打开两个终端,一个是用户jack,一个是newuser: 然后我们jack作为主机,re ...

  10. [python爬虫]简单爬虫功能

    在我们日常上网浏览网页的时候,经常会看到某个网站中一些好看的图片,它们可能存在在很多页面当中,我们就希望把这些图片保存下载,或者用户用来做桌面壁纸,或者用来做设计的素材. 我们最常规的做法就是通过鼠标 ...