C++~~~std::sort
2011-07-29 02:00
375 查看
template<class RandomAccessIterator> void sort( RandomAccessIterator first, RandomAccessIterator last ); template<class RandomAccessIterator, class Predicate> void sort( RandomAccessIterator first, RandomAccessIterator last, Predicate comp );
// alg_sort.cpp // compile with: /EHsc #include <vector> #include <algorithm> #include <functional> // For greater<int>( ) #include <iostream> // Return whether first element is greater than the second bool UDgreater ( int elem1, int elem2 ) { return elem1 > elem2; } int main( ) { using namespace std; vector <int> v1; vector <int>::iterator Iter1; int i; for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 2 * i ); } int ii; for ( ii = 0 ; ii <= 5 ; ii++ ) { v1.push_back( 2 * ii + 1 ); } cout << "Original vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; sort( v1.begin( ), v1.end( ) ); cout << "Sorted vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; // To sort in descending order. specify binary predicate sort( v1.begin( ), v1.end( ), greater<int>( ) ); cout << "Resorted (greater) vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; // A user-defined (UD) binary predicate can also be used sort( v1.begin( ), v1.end( ), UDgreater ); cout << "Resorted (UDgreater) vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; }
相关文章推荐
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- C++ std::sort 函数的STL内部错误
- C++ STL std::sort分析
- C++中std::sort/std::stable_sort/std::partial_sort的区别及使用
- C++ std::sort导致的core dumped
- 关于C++各类排序算法与std::sort性能的比较
- C++的std::sort
- [C/C++标准库]_[初级]_[使用std::sort排序各种类型数据]
- [C/C++标准库]_[初级]_[使用std::sort排序各种类型数据]
- 实战c++中的vector系列--使用sort算法对vector进行排序(对vector<string>排序、使用稳定的排序std::stable_sort())
- C语言qsort C++的sort
- C++ std::vector元素的内存分配问题(补充)
- EXCEL排序 (sort) hdu ACM 1862 c++
- C++ sort 排序(降序、升序)使用总结
- 几种C++ std::string和std::wstring相互转换的转换方法
- C++中如何去掉std::string对象的首尾空格
- c++ std - accumulate 求数组和
- 详细解说 STL 排序(Sort)_C++ Programmer's Cookbook
- C++ 中list容器,stl中sort自定义排序规则 注意事项
- C++ using namespace std 详解