STL之排序算法

97 篇文章 0 订阅
92 篇文章 0 订阅

 

Sorting
sort
Sort elements in range (function template)
stable_sort
Sort elements preserving order of equivalents (function template )
partial_sort
Partially Sort elements in range (function template)
partial_sort_copy
Copy and partially sort range (function template)
nth_element
Sort element in range (function template)

 

其中sort,和stable_sort在我的多篇博客出现过,在此不再累赘,想复习sort和stable_sort的朋友,可以翻看我的博客

1partial_sort

template <class RandomAccessIterator>
  void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,
                      RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void partial_sort ( RandomAccessIterator first, RandomAccessIterator middle,
                      RandomAccessIterator last, Compare comp );

以上是它的函数原型,它是根据你给的中间地址,然后部分排序,比如你给的地址是 v.begin(),v.begin()+5,v.end(),其中v的大小是10那么结果就是前面五个排序了,后面五个按照原来的顺序,看代码


 

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
	int a[]={10,9,8,7,6,5,4,3,2,1};
	vector<int>v(a,a+10);
	partial_sort(v.begin(),v.begin()+5,v.end());
	for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){
		cout<<*itera<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
bool isBigger(int a,int b){
	return a>b;
}
int main(){
	int a[]={1,2,3,4,5,6,7,8,9,10};
	vector<int>v(a,a+10);
	partial_sort(v.begin(),v.begin()+5,v.end(),isBigger);
	for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){
		cout<<*itera<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}

代码简单,不必解释了吧。

2partial_sort_copy,

函数原型如下

template <class InputIterator, class RandomAccessIterator>
  RandomAccessIterator
    partial_sort_copy ( InputIterator first,InputIterator last,
                        RandomAccessIterator result_first,
                        RandomAccessIterator result_last );

template <class InputIterator, class RandomAccessIterator, class Compare>
  RandomAccessIterator
    partial_sort_copy ( InputIterator first,InputIterator last,
                        RandomAccessIterator result_first,
                        RandomAccessIterator result_last, Compare comp );


其实它就相当于sort()和copy的结合,所谓的partial没有给定范围,实际上用的比较多的,还是begin--->end

看代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
int main(){
	int a[]={4,5,2,1,3,6,9,8,7,2};
	vector<int>v(a,a+10);
	vector<int>vv(10);
	partial_sort_copy(v.begin(),v.end(),vv.begin(),vv.end());
	for(vector<int>::iterator itera=vv.begin(); itera!=vv.end();++itera){
		cout<<*itera<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
bool isBigger(int a,int b){
	return a>b;
}
int main(){
	int a[]={4,5,2,1,3,6,9,8,7,2};
	vector<int>v(a,a+10);
	vector<int>vv(10);
	partial_sort_copy(v.begin(),v.end(),vv.begin(),vv.end(),isBigger);
	for(vector<int>::iterator itera=vv.begin(); itera!=vv.end();++itera){
		cout<<*itera<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}


3nth_element,STL中的nth_element()方法的使用通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的

其函数原型如下:

template <class RandomAccessIterator>
  void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,
                     RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,
                     RandomAccessIterator last, Compare comp );

看下代码:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
	int a[]={10,4,2,7,6,5,9,3,8,1};
	vector<int>v(a,a+10);
	for(vector<int>::iterator itera=v.begin();itera!=v.end();++itera){
		cout<<*itera<<" ";
	}
	cout<<endl;
	nth_element(v.begin(),v.begin()+6,v.end());
	for(vector<int>::iterator iterb=v.begin();iterb!=v.end();++iterb){
		cout<<*iterb<<" ";
	}
	cout<<endl;
	system("pause");
	return 0;
}



 


 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值