排序算法nth_element()和partition()

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

using namespace std;

int main()
{
	deque<int> ideq;
	
	for (int i = 4; i <= 8; ++i)
		ideq.push_back(i);
	for (int i = 3; i <= 7; ++i)
		ideq.push_back(i);
	for (int i = 1; i <= 5; ++i)
		ideq.push_back(i);

	for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;

	nth_element(ideq.begin(), ideq.begin() + 3, ideq.end());// nth_element 根据第n个元素进行排序,

	for (deque<int>::iterator iter = ideq.begin(); iter != ideq.begin()+4; ++iter)
		cout << *iter << ' ';
	cout << endl;

	deque<int>::iterator pos;
	pos = partition(ideq.begin(), ideq.end(), bind2nd(less_equal<int>(), 3));//partition是分区算法,在这里找小于等于3的数,
	for (deque<int>::iterator iter = ideq.begin(); iter != pos; ++iter)
		cout << *iter << ' ';
	cout << endl;


	return 0;
}

你可能感兴趣的:(排序算法nth_element()和partition())