C++ STL算法partition

C++ STL算法partition,第1张

C++ STL算法partition
函数原型
template
_NODISCARD inline _FwdIt partition_point(_FwdIt _First, _FwdIt _Last, _Pr _Pred)

根据某个筛选规则对指定范围内的数据进行分组(即符合条件的为一组,不符合条件的为另一组),并返回两组数据之间的分界位置。

参数

  • fisrt last 输入容器
  • pred 可以是函数指针或函数对象或lambda表达式
函数 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

bool isEven(int a)
{
	std::cout << "a = " << a << std::endl;
	return a % 2 == 0;
}

int main()
{	
	std::vector b{ 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
	std::partition(std::begin(b), std::end(b), isEven);
	std::copy(std::begin(b), std::end(b), std::ostream_iterator(std::cout, "; "));
	std::cout << "nn";

	std::vector::iterator it = std::partition_point(std::begin(b), std::end(b), isEven);
	if (it != std::end(b))
	{
		size_t dist = std::distance(std::begin(b), it);
		std::cout << "value: " << *it << "; distance: " << dist << std::endl;
	}

	return -1;
}

//输出
a = 1
a = 9
a = 8
a = 2
a = 3
a = 7
a = 6
a = 4
a = 5
8; 2; 6; 4; 5; 3; 7; 1; 9;

a = 5
a = 6
a = 4
value: 5; distance: 4
 仿函数
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

bool isEven(int a)
{
	std::cout << "a = " << a << std::endl;
	return a % 2 == 0;
}

class IsEven
{
public:
	IsEven(int b) :m_b(b) {}
	bool operator()(int value)
	{
		std::cout << "value = " << value << std::endl;
		return 0 == value % m_b;
	}
	int m_b;
};

int main()
{	
	std::vector b{ 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
	std::partition(std::begin(b), std::end(b), IsEven(2));
	std::copy(std::begin(b), std::end(b), std::ostream_iterator(std::cout, "; "));
	std::cout << "nn";

	std::vector::iterator it = std::partition_point(std::begin(b), std::end(b), isEven);
	if (it != std::end(b))
	{
		size_t dist = std::distance(std::begin(b), it);
		std::cout << "value: " << *it << "; distance: " << dist << std::endl;
	}

	return -1;
}
bind

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

bool isEven(int a, int b)
{
	std::cout << "a = " << a << std::endl;
	return a % 2 == 0;
}

class IsEven
{
public:
	IsEven(int b) :m_b(b) {}
	bool operator()(int value)
	{
		std::cout << "value = " << value << std::endl;
		return 0 == value % m_b;
	}
	int m_b;
};

int main()
{	
	std::vector b{ 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
	auto f1 = std::bind(isEven, std::placeholders::_1, 2);
	std::partition(std::begin(b), std::end(b), f1);
	std::copy(std::begin(b), std::end(b), std::ostream_iterator(std::cout, "; "));
	std::cout << "nn";

	auto f2 = std::bind(IsEven(2), std::placeholders::_1);
	std::vector::iterator it = std::partition_point(std::begin(b), std::end(b), f2);
	if (it != std::end(b))
	{
		size_t dist = std::distance(std::begin(b), it);
		std::cout << "value: " << *it << "; distance: " << dist << std::endl;
	}

	return -1;
}

//输出
a = 1
a = 9
a = 8
a = 2
a = 3
a = 7
a = 6
a = 4
a = 5
8; 2; 6; 4; 5; 3; 7; 1; 9;

value = 5
value = 6
value = 4
value: 5; distance: 4
lambda

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class IsEven
{
public:
	IsEven(int b) :m_b(b) {}
	bool operator()(int value)
	{
		std::cout << "value = " << value << std::endl;
		return 0 == value % m_b;
	}
	int m_b;
};

int main()
{	
	std::vector b{ 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
	std::partition(std::begin(b), std::end(b), [](int b) { return 0 == b % 2; });
	std::copy(std::begin(b), std::end(b), std::ostream_iterator(std::cout, "; "));
	std::cout << "nn";

	auto f2 = std::bind(IsEven(2), std::placeholders::_1);
	std::vector::iterator it = std::partition_point(std::begin(b), std::end(b), f2);
	if (it != std::end(b))
	{
		size_t dist = std::distance(std::begin(b), it);
		std::cout << "value: " << *it << "; distance: " << dist << std::endl;
	}

	return -1;
}

//输出
8; 2; 6; 4; 5; 3; 7; 1; 9;

value = 5
value = 6
value = 4
value: 5; distance: 4

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存