C++的STL中提供了很强大的排序函数sort,可以对任意数组,结构体及类进行排序,下面我们先来看最简单的数组排序。默认的升序排列,我们也可以在后面加上less或greater来告诉编译器我们想要的排序顺序。

vector<int> v = {, , , , , , };

// Ascending order
sort(v.begin(), v.end());
sort(v.begin(), v.end(), less<int>()); // Descending order
sort(v.rbegin(), v.rend());
sort(v.begin(), v.end(), greater<int>());

如果是一个二维数组,也可以是用sort,我们可以选择根据某一列来进行排序,如果我们不重写cmp函数,那么默认的是根据第一列来排序,当然我们可以通过重写来根据其他列来排序:

/* Input matrix
m = [
1 4 2
0 8 3
3 5 1
]
*/ // Ascending order by first column
sort(m.begin(), m.end());
/*
m = [
0 8 3
1 4 2
3 5 1
]
*/ // Descending order by first column
sort(m.rbegin(), m.rend());
/*
m = [
3 5 1
1 4 2
0 8 3
]
*/ // Ascending order by second column
sort(m.begin(), m.end(), [](const vector<int> &a, const vector<int> &b) { return a[] < b[]; } ); bool cmp(const vector<int> &a, const vector<int> &b) {
return a[] > b[];
}
sort(m.begin(), m.end(), cmp);
/*
m = [
1 4 2
3 5 1
0 8 3
]
*/ // Descending order by second column
sort(m.begin(), m.end(), [](const vector<int> &a, const vector<int> &b) { return a[] > b[]; } ); bool cmp(const vector<int> &a, const vector<int> &b) {
return a[] < b[];
}
sort(m.begin(), m.end(), cmp);
/*
m = [
0 8 3
3 5 1
1 4 2
]
*/

下面来看如何给class排序,我们将自定义的类class装入到容器vector中,然后根据class中的一个参数为依据进行排序,这里分两种情况,一个是vector中装指针的class,一种的装正常的class,它们再重写cmp比较函数上有些区别的,参见代码如下:

class A {
public:
int a1, a2;
A(int m, int n): a1(m), a2(n) {}
}; class B {
public:
int b1, b2;
B(int m, int n): b1(m), b2(n) {}
}; bool cmp1(A const *a, A const *b) {
return a->a1 < b->a1;
} bool cmp2(B const &a, B const &b) {
return a.b1 < b.b1;
} void printArray(vector<A*> array) {
for (int i = ; i < array.size(); ++i) {
cout << array[i]->a1 << " " << array[i]->a2 << endl;
}
cout << endl;
} void printArray2(vector<B> array) {
for (int i = ; i < array.size(); ++i) {
cout << array[i].b1 << " " << array[i].b2 << endl;
}
cout << endl;
} int main() { vector<A*> array;
array.push_back(new A(, ));
array.push_back(new A(, ));
array.push_back(new A(, ));
array.push_back(new A(, ));
array.push_back(new A(, ));
array.push_back(new A(, )); printArray(array);
sort(array.begin(), array.end(), cmp1);
printArray(array); vector<B> array2;
array2.push_back(B(, ));
array2.push_back(B(, ));
array2.push_back(B(, ));
array2.push_back(B(, ));
array2.push_back(B(, ));
array2.push_back(B(, )); printArray2(array2);
sort(array2.begin(), array2.end(), cmp2);
printArray2(array2); return ;
}

C++ sort vector<vector<int> > or vector<MyClass> 容器的排序的更多相关文章

  1. const vector<int> 和 vector<const int>问题讨论

    1.const vector <int> vec(10) —— 与const int a[10]是一回事,意思是vec只有10个元素,不能增加了,里面的元素也是不能变化的 vector&l ...

  2. CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  3. vector向量容器元素排序与查找

    1.利用标准库函数sort()对vector进行排序 参考源码: #include <algorithm> #include <vector> vector<int> ...

  4. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  5. 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())

    关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为 ...

  6. Java中Array.sort()的几种用法(需要初始化要排序的对象)

    ====================================================== 1.Arrays.sort(int[] a) 这种形式是对一个数组的所有元素进行排序,并且 ...

  7. 使用泛型实现对int数组或者String数组进行排序

    因为是使用的泛型,我们并不确定数据类型, 对于数据的比较就不能用平时的大于或者小于. 我们需要比较对象实现Comparable接口,该接口下的compareTo()方法可以用来比大小 定义Sort类: ...

  8. Codeforces Global Round 6D(VECTOR<ARRAY<INT,3> >)

    一个人只要存在债务关系,那么他的债务可以和这整个债务关系网中任何人连边,和他当初借出或欠下的人没有关系.只需要记录他的债务值即可. #define HAVE_STRUCT_TIMESPEC #incl ...

  9. hdu 4557 非诚勿扰 vector的应用 原来vector 可以删除指定位置元素 不过消耗大

    非诚勿扰 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submi ...

随机推荐

  1. 《ASP.NET1200例》统计网站访问量源代码

    void Application_Start(object sender, EventArgs e)     {        //在应用程序启动时运行的代码        int count=0;  ...

  2. Compare Strings

    Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...

  3. Linux set env export declare unset

    http://www.it165.net/os/html/201405/8390.html env /etc/profile 环境变量 系统提供 可改 set /etc/bashrc及用户自定义的变量 ...

  4. 转mysql复制主从集群搭建

    最近搭了个主从复制,中间出了点小问题,排查搞定,记录下来 1环境:虚拟机:OS:centos6.5Linux host2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 ...

  5. google maps js v3 api教程(1) -- 创建一个地图

    原文地址 google maps javascript官方文档:https://developers.google.com/maps/documentation/javascript/ 在创建地图之前 ...

  6. ecshop绕过验证码暴力破解

    若验证码不匹配,并没有销毁当前验证码   所以就可以一次请求验证码图片后,只要不再刷新验证码就可以一直使用  1.获取正确的验证码   2. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  7. Ubuntu 下搭建SVN服务器

    root@iZ25q0jd99eZ:~# sudo apt-get install subversion root@iZ25q0jd99eZ:/etc/subversion# mkdir /svn r ...

  8. [转]C# Winform ListView使用

    以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...

  9. SURF算法与源码分析、上

    如果说SIFT算法中使用DOG对LOG进行了简化,提高了搜索特征点的速度,那么SURF算法则是对DoH的简化与近似.虽然SIFT算法已经被认为是最有效的,也是最常用的特征点提取的算法,但如果不借助于硬 ...

  10. hdu 4640(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4640 思路:f[i][j]表示一个人状态i下走到j的最小花费,dp[i][j]表示i个人在状态j下的最 ...