STL常用排序算法

排序和通用算法(14):提供元素排序策略


函数名

头文件

函数功能

inplace_merge

<algorithm>

合并两个有序序列,结果序列覆盖两端范围。重载版本使用输入的操作进行排序

 

函数原形

template<class BidIt> void inplace_merge(BidIt first, BidIt middle, BidIt last);

 

 

template<class BidIt, class Pred> void inplace_merge(BidIt first, BidIt middle, BidIt last, Pred pr);

merge

<algorithm>

合并两个有序序列,存放到另一个序列。重载版本使用自定义的比较

 

函数原形

template<class InIt1, class InIt2, class OutIt> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);

 

 

template<class InIt1, class InIt2, class OutIt, class Pred> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x, Pred pr);

nth_element

<algorithm>

将范围内的序列重新排序,使所有小于第n个元素的元素都出现在它前面,而大于它的都出现在后面。重载版本使用自定义的比较操作

 

函数原形

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

 

 

template<class RanIt, class Pred> void nth_element(RanIt first, RanIt nth, RanIt last, Pred pr);

partial_sort

<algorithm>

对序列做部分排序,被排序元素个数正好可以被放到范围内。重载版本使用自定义的比较操作

 

函数原形

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

 

 

template<class RanIt, class Pred> void partial_sort(RanIt first, RanIt middle, RanIt last, Pred pr);

partial_sort_copy

<algorithm>

与partial_sort类似,不过将经过排序的序列复制到另一个容器

 

函数原形

template<class InIt, class RanIt> RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2);

 

 

template<class InIt, class RanIt, class Pred> RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2, Pred pr);

partition

<algorithm>

对指定范围内元素重新排序,使用输入的函数,把结果为true的元素放在结果为false的元素之前

 

函数原形

template<class BidIt, class Pred> BidIt partition(BidIt first, BidIt last, Pred pr);

random_shuffle

<algorithm>

对指定范围内的元素随机调整次序。重载版本输入一个随机数产生操作

 

函数原形

template<class RanIt> void random_shuffle(RanIt first, RanIt last);

 

 

template<class RanIt, class Fun> void random_shuffle(RanIt first, RanIt last, Fun& f);

reverse

<algorithm>

将指定范围内元素重新反序排序

 

函数原形

template<class BidIt> void reverse(BidIt first, BidIt last);

reverse_copy

<algorithm>

与reverse类似,不过将结果写入另一个容器

 

函数原形

template<class BidIt, class OutIt> OutIt reverse_copy(BidIt first, BidIt last, OutIt x);

rotate

<algorithm>

将指定范围内元素移到容器末尾,由middle指向的元素成为容器第一个元素

 

函数原形

template<class FwdIt> void rotate(FwdIt first, FwdIt middle, FwdIt last);

rotate_copy

<algorithm>

与rotate类似,不过将结果写入另一个容器

 

函数原形

template<class FwdIt, class OutIt> OutIt rotate_copy(FwdIt first, FwdIt middle, FwdIt last, OutIt x);

sort

<algorithm>

以升序重新排列指定范围内的元素。重载版本使用自定义的比较操作

 

函数原形

template<class RanIt> void sort(RanIt first, RanIt last);

 

 

template<class RanIt, class Pred> void sort(RanIt first, RanIt last, Pred pr);

stable_sort

<algorithm>

与sort类似,不过保留相等元素之间的顺序关系

 

函数原形

template<class BidIt> void stable_sort(BidIt first, BidIt last);

 

 

template<class BidIt, class Pred> void stable_sort(BidIt first, BidIt last, Pred pr);

stable_partition

<algorithm>

与partition类似,不过不保证保留容器中的相对顺序

 

函数原形

template<class FwdIt, class Pred> FwdIt stable_partition(FwdIt first, FwdIt last, Pred pr);


#include <iostream>        
#include<string>        
#include <vector>        
#include <list>        
#include <set>        
#include <map>        
#include <algorithm>        
#include <functional>  
#include<iterator>
using namespace std;

template<typename T>
void printElement(T &t)
{
	T::iterator it;

	for (it=t.begin();it!=t.end();it++)
	{
		cout<<*it<<" ";
	}
}


void display()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	vector<int> v1;
	v1.push_back(5);
	v1.push_back(7);
	v1.push_back(9);
	vector<int> v3;
	v3.resize(v.size()+v1.size());

	vector<int>::iterator it = merge(v.begin(),v.end(),v1.begin(),v1.end(),v3.begin());

	//cout<<*(it--)<<endl;

	printElement(v3);
}

class Teacher
{
public:
	Teacher()
	{
		age = 20;
		name ="ABC";
	}
	Teacher(string name,int age)
	{
		this->name = name;
		this->age = age;
	}
	~Teacher()
	{

	}
	int getAge()
	{
		return age;
	}
	string getName()
	{
		return name;
	}
	void printT()
	{
		cout<<this->name<<":"<<this->age<<endl;
	}
protected:
private:
	int age;
	string name;
};

//按照年龄从小到大排序
bool CompareTeacher(Teacher &t1,Teacher &t2)
{
	if (t1.getAge()<t2.getAge())
	{
		return true;
	}
	return false;
};
void display2()
{
	Teacher t1("t1",38),t2("t2",32),t3("t3",29),t4("t4",35);
	vector<Teacher> v;
	v.push_back(t1);
	v.push_back(t2);
	v.push_back(t3);
	v.push_back(t4);

	cout<<"排序前"<<endl;
	for (vector<Teacher>::iterator it = v.begin();it!=v.end();it++)
	{
		it->printT();
	}
	//sort根据自定义函数对象,进行自定义数据类型排序
	//体会算法的统一性 实现了算法和数据类型的类型分离===》函数对象
	sort(v.begin(),v.end(),CompareTeacher);

	cout<<"排序后"<<endl;
	for (vector<Teacher>::iterator it = v.begin();it!=v.end();it++)
	{
		it->printT();
	}
}

void display3()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);
	cout<<"倒序前"<<endl;
	printElement(v);
   //对v进行倒序
	reverse(v.begin(),v.end());
	cout<<endl<<"倒序后"<<endl;
	printElement(v);

	cout<<endl<<"随机前"<<endl;
	printElement(v);
	random_shuffle(v.begin(),v.end());
	cout<<endl<<"随机后"<<endl;
	printElement(v);

	string str1 = "abcdefj";

	cout<<endl<<"str1随机前:"<<str1<<endl;

	random_shuffle(str1.begin(),str1.end());

	cout<<"str1随机后:"<<str1<<endl;
}
int main()
{
	display3();
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值