C++ sort() / partial_sort() / partial_sort_copy排序函数用法

来源:互联网 发布:双色球蓝球矩阵图说明 编辑:程序博客网 时间:2024/05/20 17:25

本文仅仅发布在CSDN我的博客:点击打开链接。

http://blog.csdn.net/pythontojava?viewmode=contents

转载请注明出处。

我用的IDE是VS2013.。代码在vc++6.0不能编译,要把int _tmain(int argc, _TCHAR* argv[]) 改成 int main() 。

====分割线=========

一、sort()排序函数的头文件是<algorithm>,函数对元素进行升序排序。

用法:

1.sort (RandomAccessIterator first, RandomAccessIterator last);

2.sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

更多资料点这里:http://www.cplusplus.com/reference/algorithm/sort/

// sort.cpp : 定义控制台应用程序的入口点。//IDE是vs2013#include "stdafx.h"#include <iostream>#include <algorithm>    // sort()头文件#include <vector>bool compare(int i, int j)//自定义排序规则递减{return(i > j);}using namespace std;int _tmain(int argc, _TCHAR* argv[]){int myints[] = { 32, 71, 12, 45, 26, 80, 53, 33 };vector<int>v(myints, myints + 8);sort(v.begin(), v.begin() + 8);//默认递增排序auto i = v.begin();for (; i != v.end(); i++){cout << *i << ' ';}cout << endl;sort(v.begin(), v.begin() + 8,compare);//递减排序for (auto value : v){cout << value << ' ';}system("pause");return 0;}
运行结果:

========分割线========

二、partial_sort()函数,进行部分排序。

用法:

1.partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);

2.partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);

更多资料点这里:http://www.cplusplus.com/reference/algorithm/partial_sort/

// partial_sort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<algorithm>#include<vector>using namespace std;bool compare(int a, int b){return(a > b);//降序}int _tmain(int argc, _TCHAR* argv[]){int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };vector<int>vec(myints, myints + 9);partial_sort(vec.begin(), vec.begin() + 5, vec.end());//对第0~5个元素进行排序for (auto i : vec){cout << i << ' ';}system("pause");return 0;}
运行结果:


========分割线=========

三、partial_sort_copy(),对部分元素进行排序,并复制到另一个容器里。

用法:

1.partial_sort_copy (InputIterator first,InputIterator last,RandomAccessIterator result_first,RandomAccessIterator result_last);

2.partial_sort_copy (InputIterator first,InputIterator last,RandomAccessIterator result_first,RandomAccessIterator result_last, Compare comp);

(待排序的容器首指针,尾指针,接收元素的容器的首指针,尾指针)

// partial_sort_copy.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>     // std::cout#include <algorithm>    // std::partial_sort_copy#include <vector>       // std::vectorusing namespace std;bool compare(int i, int j){return (i>j);}int _tmain(int argc, _TCHAR* argv[]){int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };vector<int> vec(5);partial_sort_copy(myints, myints + 9, vec.begin(), vec.end());cout << "vec contains:";for (auto it : vec){std::cout << ' ' << it;}std::cout << '\n';system("pause");return 0;}
运行结果:

=======分割线==========

四、is_sorted(),判断一系列元素是否有序

// is_sorted.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<algorithm>#include<array>using namespace std;int _tmain(int argc, _TCHAR* argv[]){array<int, 5> ar = { 1, 2, 3, 4, 5 };cout << is_sorted(ar.begin(), ar.end());//判断一系列元素是否有序,是就返回1cout << endl;system("pause");return 0;}
运行结果:

=======分割线==========

五、is_sorted_until(),返回有序元素后面无序的第一个元素的指针

用法:

1.is_sorted_until (ForwardIterator first, ForwardIterator last);

2.is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp);//Compare comp自定义比较规则

#include "stdafx.h"#include <iostream>     // std::cout#include <algorithm>    // std::is_sorted_until, std::prev_permutation#include <array>using namespace std;int _tmain(int argc, _TCHAR* argv[]){array<int, 4> foo={2,3,-5,0};//2、3有序,-5无序array<int, 4>::iterator it;it =is_sorted_until(foo.begin(), foo.end());//返回指向-5的指针cout << *it;cout << endl;system("pause");return 0;}
运行结果:


0 0
原创粉丝点击