如何写一个n-ary否定器?

时间:2017-01-23 10:51:42

标签: c++ functor adaptor

一元和二元否定的有用性很容易理解。

一元否定的示例( not1 ):

class Even
{
public:
    bool operator() (const int& x) const { return x % 2 == 0; }
    typedef int argument_type;
};

int values[] = { 9, 1, 8, 2, 7, 3, 6, 4, 5 };

int even = count_if(values, values + 9, Even());
int odd = count_if(values, values + 9, not1(Even())); // <= unary negator
cout << "We have " << even << " even elements in the array.\n";
cout << "We have " << odd  << " odd elements in the array.\n";

输出:

We have 4 even elements in the array.
We have 5 odd elements in the array.

二元否定符示例( not2 ):

int values[] = { 9, 1, 8, 2, 7, 3, 6, 4, 5 };

// original array
for (int i : values)
    cout << i << " ";
cout << "\n";


// array in ascending order
sort(values, values + 9, less<int>());

for (int i : values)
    cout << i << " ";
cout << "\n";

// array in descending order
sort(values, values + 9, not2(less<int>())); // <= binary negator

for (int i : values)
    cout << i << " ";
cout << "\n\n";

输出:

9 1 8 2 7 3 6 4 5
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1

n-ary negators( not3,not4,not5 ... notn )怎么样?

假设我需要计算集合(可能是数组)中不在两个数字(下限和上限)之间的元素数量。

.

int elems_betweem = count_if(values, values + n, not3(bind(Between<int>(), _1, lowerValue, upperValue)));

.

如何编写 not3 否定符?

更重要的是,我们是否具有通用not作为not1not2的替换者,与bind vs bind1st和{{ 1}}?

谢谢

3 个答案:

答案 0 :(得分:1)

由于C ++ 17 std::not_fn可用:

auto between = [](int value, int lowerValue, int upperValue) {
    return lowerValue < value && value < upperValue;
};

int elems_between = std::count_if(std::cbegin(values), std::cend(values),
    std::bind(std::not_fn(between), std::placeholders::_1, lowerValue, upperValue));

wandbox example

答案 1 :(得分:0)

  

如何编写not3否定符?

符合C ++ 03的示例:

template<class Pred>
class ternary_predicate {
    Pred pred;
public:
    ternary_predicate(Pred pred): pred(pred){}

    template<class A, class B, class C>
    bool
    operator()(A const& a, B const& b, C const& c) const {
        return !pred(a, b, c);
    }
};

template<class Pred>
ternary_predicate<Pred>
not3(Pred pred) {
    return ternary_predicate<Pred>(pred);
}
  

更重要的是,我们是否有一个泛型而不是像bind与bind1st和bind2nd一样的not1和not2的替换者?

一旦C ++ 17正式发布,我们将会这样做。建议引入std::not_fn作为std::not1std::not2的替代,然后弃用。{/ p>

如果你感到不耐烦,那么在C ++ 14中实现自己并不困难:

template<class Pred>
auto
not_fn(Pred&& pred) {
    return [pred=std::forward<Pred>(pred)](auto&&... args){
        return !pred(std::forward<decltype(args)>(args)...);
    };
}

答案 2 :(得分:0)

您可以自己编写std::not_fn,只要它不可用:

template <typename T>
struct not_func
{
    template <typename...Args>
    constexpr bool operator()(const Args&...args) const { return !T{}(args...); }
};

使用示例:

int main()
{

    bool a1 = std::less<int>{}(1, 2);
    bool a2 = not_func<std::less<int>>{}(1, 2);

    bool b1 = Even{}(1);
    bool b2 = not_func<Even>{}(1);


    std::cout
        << "a1 = " << (a1 ? "true" : "false") << "\n"
        << "a2 = " << (a2 ? "true" : "false") << "\n"
        << "b1 = " << (b1 ? "true" : "false") << "\n"
        << "b2 = " << (b2 ? "true" : "false") << "\n";
}

[On Coliru] [On Godbolt]

我还没有测试过所有可能的变种,所以这可能仍然是错误的。