STL算法库-排序和相关操作(一)

一、定义
通过对容器中元素的赋值和变换,改变元素顺序,不适合关联式容器
二、全部元素排序
该排序算法只支持随机存储迭代器,因此只适合vector和deque型容器,不适合list容器,list有自己的成员函数sort进行排序
原型:sort(iterator begin,iterator end);
定义:默认升序排序
原型:sort(iterator begin,iterator end,Pred pr);
定义:根据pr的值进行升序或者降序排序

原型:stable_sort(iterator begin,iterator end);
定义:默认升序排序
原型:stable_sort(iterator begin,iterator end,Pred pr);
定义:根据pr的值进行升序或者降序排序

示例:

#include "stdafx.h"
#include "iostream"
#include "algorithm"
#include "deque"
#include "vector"
#include "iterator"
using namespace std;

class myCom
{
public:
 bool operator()(const int& i,const int& j)
 {
  return i>j;
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 myCom mc;
 vector<int> v1,v2;
 int dim[]={1,3,2,4,5,9,7,8,6};
 v1.assign(dim,dim+9);
 v2 = v1;
 cout<<"vector v1:";
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 stable_sort(v1.begin(),v1.end());
 cout<<"默?认?升y序?排?序?后?";
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 cout<<"降?序?排?序?后?";
 sort(v1.begin(),v1.end(),mc);
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 return 0;
}

三、局部排序
用partical_sort排序后,排列有序的元素放在容器的前面,非已序元素放到容器的后面。
原型:partial_sort(iterator first,iterator last,iterator end);
定义:对区间[first,last]内元素进行排序,排序后保证该区间元素为有序状态,若last和end相等,则是全部排序。
原型:partical_sort(iterator first,iterator last,iterator end,Pred pr);
定义:对区间[first,last]内的元素按照pr定义的规则进行排序,排序后保证该区间元素为有序状态,若last和end相等,则全部元素进行排序

原型:partial_sort_copy(iterator1 first1,iterator1 last1,iterator2 first2,iterator2 last2);
定义:将区间[first1,last1]复制到目标区间[first2,last2],同时对这部分元素进行排序,函数返回复制的最后一个元素的下一个位置
原型:partical_sort_copy(iterator1 first1,iterator1 last1,iterator2 first2,iterator2 last2,Pred pr)
定义:将区间[first1,last1]复制到目标区间[first2,last2],同时对这部分元素进行按照pr排序,函数返回复制的最后一个元素的下一个位置

示例:

#include "stdafx.h"
#include "iostream"
#include "algorithm"
#include "deque"
#include "vector"
#include "iterator"
using namespace std;

class myCom
{
public:
 bool operator()(const int& i,const int& j)
 {
  return i>j;
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 myCom mc;
 vector<int> v1,v2;
 int dim[]={1,3,2,4,5,9,7,8,6};
 v1.assign(dim,dim+9);
 v2 = v1;
 cout<<"vector v1:";
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 partial_sort(v1.begin(),v1.begin()+3,v1.end());
 cout<<"默?认?升y序?排?序?后?";
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 cout<<"降?序?排?序?后?";
 partial_sort_copy(v1.begin(),v1.begin()+2,v2.begin()+1,v2.begin()+3,mc);
 copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 return 0;
}

四、根据某个元素进行排序
原型:nth_element(iterator first,iterator n,iterator last);
原型:nth_element(iterator first,iterator n,iterator last,Pred pr);
定义:对指定区间内的元素进行排序,并且使第n个位置上的元素就位,即所在位置n之前的元素都小于等于它,所在位置n之后的元素都大于等于它
示例:

#include "stdafx.h"
#include "iostream"
#include "algorithm"
#include "deque"
#include "vector"
#include "iterator"
using namespace std;

class myCom
{
public:
 bool operator()(const int& i,const int& j)
 {
  return i>j;
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
 myCom mc;
 vector<int> v1,v2;
 int dim[]={1,3,4,2,5,9,7,8,6,0};
 v1.assign(dim,dim+10);
 v2 = v1;
 cout<<"vector v1:";
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 nth_element(v1.begin(),v1.begin()+3,v1.begin()+5);
 cout<<"默?认?升y序?排?序?后?";
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 cout<<"降?序?排?序?后?";
 nth_element(v1.begin(),v1.begin()+2,v1.end(),mc);
 copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
 return 0;
}

五、堆应用排序
堆应用于排序,是一种特别的元素组织方式,是以序列式群集实作而成的二叉树,它具有两大性质:堆中第一个元素的值总是最大(或最小);能够在对数时间内增加或者移除一个元素
5.1 make_heap()算法
原型:make_heap(iterator first,iterator last)
原型:make_heap(iterator first,iterator last,Pred pr)
定义:将[first,last]的元素转换成堆,参数pr是二元判断式,是排序准则。默认是大顶堆,可用排序规则改变成小顶堆;再次调用该函数时,会打乱堆顺序。

5.2 push_heap()算法
原型:push_heap(iterator first,iterator last)
原型:push_heap(iterator first,iterator last,Pred pr)
定义:在push_back()前调用该函数,可以保证在容器中元素,按照堆顺序在后面push_back数据

5.3 pop_back()算法
原型:pop_back(iterator begin,iterator end);
原型:pop_back(iterator begin,iterator end,Pred pr)
定义:在pop_back()前调用该函数,可以保证容器中元素按照堆顺序,删除堆顶元素

示例:

#include "stdafx.h"
#include "iostream"
#include "vector"
#include "iterator"
#include "algorithm"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 vector<double> v1;
 double dim[]={1.2,4.3,5.6,2.3,7.8,8.2,4.2,8.1,5.3};
 int cnt=sizeof(dim)/sizeof(double);
 v1.assign(dim,dim+cnt);
 cout<<"原-始?的?数y据Y序?列D如?下?:"<<endl;
 copy(v1.begin(),v1.end(),ostream_iterator<double>(cout," "));
 cout<<endl;
 //make_heap()
 make_heap(v1.begin(),v1.end());
 cout<<"The heap is below made by the vector v1:"<<endl;
 copy(v1.begin(),v1.end(),ostream_iterator<double>(cout," "));
 cout<<endl;
 //pop_heap()
 pop_heap(v1.begin(),v1.end());//将?堆?顶?移?到?最?后?的?位?置?
 v1.pop_back();//移?除y最?后?的?元a素?
 cout<<"the heap is below which the first element has been erased : "<<endl;
 copy(v1.begin(),v1.end(),ostream_iterator<double>(cout," "));
 cout<<endl;
 //push_heap()
 v1.push_back(13.2);
 push_heap(v1.begin(),v1.end());
 cout<<"the heap is below which the new elemment has been pushed : "<<endl;
 copy(v1.begin(),v1.end(),ostream_iterator<double>(cout," "));
 cout<<endl;
 //sort_heap;after sort_heap,the vector will not be a heap
 sort_heap(v1.begin(),v1.end());
 cout<<"sort_heap() again : "<<endl;
 copy(v1.begin(),v1.end(),ostream_iterator<double>(cout," "));
 cout<<endl;
 return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值