实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
来源:互联网 发布:iphone5susb共享网络 编辑:程序博客网 时间:2024/04/28 08:19
写了挺多关于vector的操作了,正好工作中遇到对vector进行排序的问题,这里就讨论一下。
直接使用sort算法,那就先了解一下:
template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class Compare> 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 <iostream> // std::cout#include <algorithm> // std::sort#include <vector> // std::vector#include <string>bool myfunction(int i, int j) { return (i<j); }struct myclass { bool operator() (int i, int j) { return (i<j); }} myobject;int main() { int myints[] = { 32,71,12,45,26,80,53,33 }; std::vector<int> myvector(myints, myints + 8);// 32 71 12 45 26 80 53 33 // using default comparison (operator <) std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71)26 80 53 33 // using function as comp std::sort(myvector.begin() + 4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp std::sort(myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; // Sorting the string vector std::vector<std::string> stringVec = { "John", "Bob", "Joe", "Zack", "Randy" }; sort(stringVec.begin(), stringVec.end()); for (std::string &s : stringVec) std::cout << s << " "; return 0;}//输出:myvector contains: 12 26 32 33 45 53 71 80Bob Joe John Randy Zack
这个时候可以使用std::stable_sort()来实现稳定的排序了:
Sorts the elements in the range [first,last) into ascending order, like sort, but stable_sort preserves the relative order of the elements with equivalent values.
#include <iostream> // std::cout#include <algorithm> // std::stable_sort#include <vector> // std::vectorbool compare_as_ints(double i, double j){ return (int(i)<int(j));}int main() { double mydoubles[] = { 3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 }; std::vector<double> myvector; myvector.assign(mydoubles, mydoubles + 8); std::cout << "using default comparison:"; std::stable_sort(myvector.begin(), myvector.end()); for (std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; myvector.assign(mydoubles, mydoubles + 8); std::cout << "using 'compare_as_ints' :"; std::stable_sort(myvector.begin(), myvector.end(), compare_as_ints); for (std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}//输出:using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
2 0
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- 实战c++中的vector系列--使用sort算法对vector<unique_ptr<string>>进行排序(sort函数“应输入 2 个参数,却提供了 3 个)
- sort 对vector排序
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 使用algorithm中的sort进行vector的自定义排序
- 对vector成员进行排序
- C++ 对vector进行排序
- std list/vector sort 排序
- 对vector中的数据排序
- std::vector的排序
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- 使用直接插入排序原理对自定义的Vector进行排序
- 用sort对vector排序(转载)
- 使用vector进行排序插入
- 使用vector list等容器对字符串的排序
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
- 对vector等STL容器进行排序
- C语言 第一章
- 求最大子矩阵的大小 + 栈
- 黑马程序员--反射应用和动态代理
- 结构体的内存存储细节
- [Pythong标准库]array——固定类型数据序列
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- JNDI与JDBC比较以及连接池的技术
- many-to-one各属性含义
- day2
- 【ShawnZhang】带你看数据结构——第十五课:哈夫曼树
- opencv的类型和方法在不同语言中的对应关系
- 击中击不中变换
- day3
- Android系统的基本架构