C++STL算法篇partition、stable_partition将满足条件的元素向前搬移算法

partition(beg,end,op)和stable_partition(beg,end,op)的特点

1:迭代器类型:双向迭代器
2:返回值:双向迭代器------指向第一个让op值为false的元素(搬移后)
3:算法功能:将[beg,end)中满足op(elem)为true和false的元素交换位置,stable_partition会保持元素的相对次序
4:复杂度:线性复杂度,后者在内存不够时复杂度为 n*log(n)

#include
#include
#include
#include
using namespace std;
int main()
{
	vector<int>coll = { 1,2,3,4,5,6,7,8,9,10};
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout << endl;

	partition(coll.begin(), coll.end(), [](int i)->bool { return i > 5; });

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	
}
#include
#include
#include
#include
using namespace std;
int main()
{
	vector<int>coll = { 1,2,3,4,5,6,7,8,9,10};
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout << endl;

	
	stable_partition(coll.begin(), coll.end(), [](int i)->bool { return i > 5; });

	copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	
}

你可能感兴趣的:(C++学习笔记)