首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::unary_function

Defined in header <functional>

template <typename ArgumentType, typename ResultType> struct unary_function;

(until C++17)(deprecated since c++11)

unary_function是一个基类,用于使用一个参数创建函数对象。

unary_function没有定义operator();预计派生类将定义这一点。unary_function只提供两种类型-argument_typeresult_type-由模板参数定义。

一些标准库函数对象适配器,如std::not1,要求它们所适应的函数对象具有特定类型的定义;std::not1要求将函数对象调整为具有名为argument_type派生函数对象,这些对象接受一个参数。unary_function是使它们与这些适配器兼容的简单方法。

unary_function在C++11中被废弃。

成员类型

Type

Definition

argument_type

ArgumentType

result_type

ResultType

二次

代码语言:javascript
复制
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
 
struct less_than_7 : std::unary_function<int, bool>
{
    bool operator()(int i) const { return i < 7; }
};
 
int main()
{
    std::vector<int> v;
    for (int i = 0; i < 10; ++i) v.push_back(i);
 
    std::cout << std::count_if(v.begin(), v.end(), std::not1(less_than_7()));
 
    /* C++11 solution:
        // Cast to std::function<bool (int)> somehow - even with a lambda
        std::cout << std::count_if(v.begin(), v.end(),
            std::not1(std::function<bool (int)>([](int i){ return i < 7; }))
        );
    */
}

二次

产出:

二次

代码语言:javascript
复制
3

二次

另见

function (C++11)

wraps callable object of any type with specified function call signature (class template)

ptr_fun (until C++17)

creates an adaptor-compatible function object wrapper from a pointer to function (function template)

pointer_to_unary_function (until C++17)

adaptor-compatible wrapper for a pointer to unary function (class template)

binary_function (until C++17)

adaptor-compatible binary function base class (class template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券