实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
2015-12-22 21:13
946 查看
写了挺多关于vector的操作了,正好工作中遇到对vector进行排序的问题,这里就讨论一下。
直接使用sort算法,那就先了解一下:
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).
也就是所说的不稳定排序。
直接上代码:
这个时候可以使用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.
直接使用sort算法,那就先了解一下:
[code]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).
也就是所说的不稳定排序。
直接上代码:
[code]#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 80 Bob 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.
[code]#include <iostream> // std::cout #include <algorithm> // std::stable_sort #include <vector> // std::vector bool 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.67 using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
相关文章推荐
- 设计模式系列(十四)组合模式(Composite Pattern)
- c++获取cpu信息代码
- C++11 并发指南二(std::thread 详解)
- C++ typedef用法小结 (※不能不看※)
- C++11 并发指南一(C++11 多线程初探)
- C语言的基础
- WINDOWS7下VC++ 6.0 无法打开文件(打开文件时软件崩溃)的解决办法。
- vc++笔记十一
- 排序(6)---------归并排序(C语言实现)
- 【c/c++】指针数组和数组指针
- n皇后(C++)
- 学习资源传送门
- C++ cout格式化输出(转)
- C语言中变量未赋值问题总结
- C语言基础
- C语言总结
- C++中placement new操作符(经典)
- Modern C++读书笔记
- 关于vc++ runtime error解决方法汇总
- C++: typename in a template template parameter