C++11标准库里有没有not1, not2的替代品?是否只能用lambda?

whoho 2014-07-01 11:40:16
新标准里由unary_function binary_function派生出来的一切,包括not1 not2 等都被声明为 deprecated
那么如何用新特性实现not1 not2的功能?能用bind函数实现吗?还是只能用lambda?

...全文
273 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
whoho 2014-07-02
  • 打赏
  • 举报
回复
引用 8 楼 taodm 的回复:
你如果这么喜欢过气的not,那boost::lamba,boost::phoneix里面大把现成的。
PS 好像好几年前在坛子里就有你出没?如果没记错的话
whoho 2014-07-02
  • 打赏
  • 举报
回复
引用 8 楼 taodm 的回复:
你如果这么喜欢过气的not,那boost::lamba,boost::phoneix里面大把现成的。
哈,我怎么把boost给忘了,谢谢提醒 我又发明了一个轮子
taodm 2014-07-02
  • 打赏
  • 举报
回复
你如果这么喜欢过气的not,那boost::lamba,boost::phoneix里面大把现成的。
whoho 2014-07-02
  • 打赏
  • 举报
回复
加上参数的perfect forwarding:

template <typename Func>
struct NegateOf
{
    NegateOf(Func f) : func(f)
    {
    }
 
    template <typename ...Args>
    bool operator() (Args&&...args)
    {
        return !func(std::forward<Args>(args)...);
    }
     
protected:
    Func func;
};
 

whoho 2014-07-02
  • 打赏
  • 举报
回复
我用C++11的特性写了一个not1 not2的workaround(struct NegateOf, helper negate_of),有需要的请参考(main内的是调用的例子)

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

template <typename Func>
struct NegateOf
{
	NegateOf(Func f) : func(f)
	{
	}

	template <typename ...Args>
	bool operator() (Args...args)
	{
		return !func(args...);
	}
	
protected:
	Func func;
};


template <typename Func>
NegateOf<Func> negate_of(Func f)
{
	return NegateOf<Func>(f);
}


int main()
{
	auto odd = [](int x)
	{
		return static_cast<bool>(x % 2);
	};

	//not1
	auto even = negate_of(odd);
	auto numbers = {3, 7, 6, 5, 4};
	auto it = std::find_if(numbers.begin(), numbers.end(), even);
	std::cout << "first even number: ";
   	if (it == numbers.end())
		std::cout << "<nil>";
	else
		std::cout << *it;
	std::cout << std::endl;

	//not2
	{
		auto neq = negate_of(std::equal_to<int>());
		std::cout << "1 neq 3? " << std::boolalpha << neq(1, 3) << std::endl;

		auto seq1 = {1, 3, 7, 4};
		auto seq2 = {2, 3, 9, 4};
		
		std::cout << "eq-compare result: \n"; 
		std::transform(
				seq1.begin(), seq1.end(), seq2.begin(),
				std::ostream_iterator<bool>(std::cout, " "),
				std::equal_to<int>());
		std::cout << std::endl;

		std::cout << "neq-compare result: \n"; 
		std::transform(
				seq1.begin(), seq1.end(), seq2.begin(),
				std::ostream_iterator<bool>(std::cout, " "),
				negate_of(std::equal_to<int>()));
		std::cout << std::endl;
	}
}


mujiok2003 2014-07-02
  • 打赏
  • 举报
回复
1. lamda更简单些, 这是语言核心扩展, bind, not1等都属于库函数扩展, 需要更多学习成本 2. 另一方面, 这些库考虑到后向兼容, 应该会予以保留。
whoho 2014-07-02
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
lambda 不是挺好的吗。
挺好,不过在原本用not1 not2的地方,会显得冗长一些
whoho 2014-07-02
  • 打赏
  • 举报
回复
引用 2 楼 dcxy0 的回复:
感觉是lambda比bind方便一些
方便是方便,但是代码行会长一点(我是说相比于能用not1的场合)
JPF1024 2014-07-02
  • 打赏
  • 举报
回复
感觉是lambda比bind方便一些
ri_aje 2014-07-02
  • 打赏
  • 举报
回复
lambda 不是挺好的吗。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧