写了挺多关于vector的操作了,正好工作中遇到对vector进行排序的问题,这里就讨论一下。
直接使用sort算法,那就先了解一下:
template
void sort (RandomAccessIterator first, RandomAccessIterator last);
template
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
Sorts the elements in the range [first,last) into ascending order.
The elements are compared using operator< for the first version, and comp for the second.
Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).
也就是所说的不稳定排序。
直接上代码:
#include // std::cout
#include // std::sort
#include // std::vector
#include
bool myfunction(int i, int j) { return (i
struct myclass {
bool operator() (int i, int j) { return (i
} myobject;
int main() {
int myints[] =