STL 拆分、排序算法

算法是线性时间的:


Partition

头文件 <algorithm>

函数功能: 根据指定的函方法重新排列数组、向量的等。

指定的方法一定是一个一元函数,并且返回值是true :  bool pred(const Type &a);

返回值是一个迭代器指向的是排序后的第二个队列的第一个元素 (Iterator to the first element of the second group.)(也可以理解成是排序后的满足条件的队列的队尾)。不过, 这个排序算法可能会改变元素的排列顺序。

示例:


bool IsOdd(int c)
{
	return (c%2) == 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> vArray;
	vArray.push_back(4);
	vArray.push_back(5);
	vArray.push_back(3);
	vArray.push_back(2);
	vArray.push_back(3);
	vArray.push_back(0);

	std::vector<int>::iterator bound;
	bound = partition(vArray.begin(), vArray.end(), IsOdd);

	cout << "vArray : \n" ;
	for (auto n : vArray)
	{
		std::cout << n << "  " ;
	}
	std::cout << std::endl << "*****************************" << endl;

	cout << "Bound : \n" ;
	for (vector<int>::iterator it = vArray.begin(); it != bound; it ++)
	{
		cout << *it << "  " ;
	}
}

输出结果 :


stable_partition

和 partition 很类似不同的是不会改变数据在队列中的顺序

示例:

bool IsOdd(int c)
{
	return (c%2) == 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> vArray;
	vArray.push_back(4);
	vArray.push_back(5);
	vArray.push_back(3);
	vArray.push_back(2);
	vArray.push_back(3);
	vArray.push_back(0);

	auto bound = stable_partition(vArray.begin(), vArray.end(), IsOdd);

	cout << "vArray : \n" ;
	for (auto n : vArray)
	{
		std::cout << n << "  " ;
	}
	std::cout << std::endl << "*****************************" << endl;

	cout << "Bound : \n" ;
	for (vector<int>::iterator it = vArray.begin(); it != bound; it ++)
	{
		cout << *it << "  " ;
	}
	cout << "\n the last : \n " ;
	std::copy(bound, std::end(vArray), std::ostream_iterator<int>(std::cout, " "));
}

 结果


nth_element

函数说明

官网示例

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>

int main()
{
	std::vector<int> v;
	v.push_back(5);
	v.push_back(6);
	v.push_back(4);
	v.push_back(3);
	v.push_back(2);
	v.push_back(6);
	v.push_back(7);
	v.push_back(9);
	v.push_back(3);
	//{5, 6, 4, 3, 2, 6, 7, 9, 3};
	std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
	std::cout << "\nThe median is " << v[v.size()/2] << '\n';
	
	std::cout << "*******************\n";
	// 从小到大排序
	std::nth_element(v.begin(), v.begin() + v.size()/2, v.end());
	std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));

	std::cout << "\nThe median is " << v[v.size()/2] << '\n';
	std::cout << "The largest element is " << v[v.size() - 1] << '\n';

	std::cout << "*******************\n";
	// 从大到小排序
	std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater<int>());
	std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
	std::cout << "\nThe second largest element is " << v[1] << '\n';


	getchar();
}

结果:

 


需要随机访问迭代器

Partial_sort


stable_sort


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值