C++ sort
来源:互联网 发布:js对联广告 编辑:程序博客网 时间:2024/05/17 08:33
之前看到过sort,今天又在程序中看到了。sort函数时STL中的函数,包含在头文件 #include <algorithm> 中。
参考: http://www.cplusplus.com/reference/algorithm/sort/
function template
<algorithm>
std::sort
template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
Sort elements in range
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).
Parameters
- first, last
- Random-access iterators to the initial and final positions of the sequence to be sorted. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable. - comp
- Binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool
. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return value
noneExample
1234567891011121314151617181920212223242526272829303132
// sort algorithm example#include <iostream> // std::cout#include <algorithm> // std::sort#include <vector> // std::vectorbool 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'; return 0;}
Output:
myvector contains: 12 26 32 33 45 53 71 80
Complexity
On average, linearithmic in the distance between first and last: Performs approximatelyN*log2(N)
(where N is this distance) comparisons of elements, and up to that many element swaps (or moves).Data races
The objects in the range[first,last)
are modified.Exceptions
Throws if any of the element comparisons, the element swaps (or moves) or the operations on iterators throws.Note that invalid arguments cause undefined behavior.
N*log2(N)
(where N is this distance) 。其中重载的时候可以定义自己的比较函数,比如让他从小到大或从大大小排列:
bool compare(int a,int b){ return a<b; //升序排列,如果改为return a>b,则为降序}#include <algorithm>int main(){ int a[20]={2,4,1,23,5,76,0,43,24,65},i; for(i=0;i<20;i++) cout<<a[i]<<endl; sort(a,a+20,compare); for(i=0;i<20;i++) cout<<a[i]<<endl; return 0;}
0 0
- Sort-C
- C++sort
- c sort
- SORT BY C
- linux sort uniq -c
- linux sort uniq -c
- C++~~~std::sort
- 【C++】STL的sort
- C ++ sort()与stable_sort()
- C++sort 函数用法
- C++: selection sort
- C语言--sort
- C++sort 函数用法
- C ++ sort()与stable_sort()
- C#ArrayList-sort
- C++STL:sort/qsort
- C++sort 函数用法
- c编写 Topological Sort
- poj-1284(Primitive Roots)(欧拉函数运用)
- windows和Linux启动过程对比
- android开发——框架理解及源码学习计划
- 注册页面(html js)
- Asp.net课后生活(5)
- C++ sort
- 设计模式(5)-对象创建型模式-Builder模式
- 使用strlcpy和strlcat
- 在win7下开启mysql服务
- Android Loaders(三)实现一个Base Loader
- POJ3130排序增量法
- asp获取当前路径语句大全
- nyoj 61 传纸条(一)双线程DP
- Asp.net课后生活(6)