std:: unary_function
| 
 | 
 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
 | 
 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
           定义于头文件
            
            
             <functional>
            
            | ||
| 
           
            
             template
            
            
             <
            
            
             typename
            
            ArgumentType,
            
             typename
            
            ResultType
            
             >
            
             struct unary_function ; | (C++11 起弃用) (C++17 中移除) | |
       
        std::unary_function
       
       是用于创建单参数函数对象的基类。
      
       
        std::unary_function
       
       并不定义
       
        
         operator
         
          (
         
         
          )
         
        
       
       ;预期派生类将定义此运算符。
       
        std::unary_function
       
       仅提供由模板参数定义的两种类型 -
       
        argument_type
       
       和
       
        result_type
       
       。
      
       某些标准库函数对象适配器,例如
       
        
         std::not1
        
       
       ,要求被适配的函数对象具有特定类型定义;
       
        
         std::not1
        
       
       要求被适配的函数对象具有名为
       
        argument_type
       
       的类型。从
       
        std::unary_function
       
       派生单参数函数对象是使其与这些适配器兼容的便捷方法。
      
       
        std::unary_function
       
       已在 C++11 中被弃用。
      
成员类型
| 类型 | 定义 | 
| 
          argument_type
          | 
          ArgumentType
          | 
| 
          result_type
          | 
          ResultType
          | 
示例
#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(10, 7); v[0] = v[1] = v[2] = 6; std::cout << std::count_if(v.begin(), v.end(), std::not1(less_than_7())); // C++11 解决方案: // 转换为 std::function<bool (int)> 的方式 - 即使使用 lambda 表达式 // std::cout << std::count_if(v.begin(), v.end(), // std::not1(std::function<bool (int)>([](int i) { return i < 7; }))); }
输出:
7
参见
| 
           
            
             
              (C++11)
             
            
           
           | 任意可复制构造可调用对象的可复制包装器 (类模板) | 
| 
           
            
             
              (C++23)
             
            
           
           | 支持给定调用签名中限定符的任意可调用对象的仅移动包装器 (类模板) | 
| 
           
            
             
              (deprecated in C++11)
             
            
            
             
              (removed in C++17)
             
            
           
           | 从函数指针创建适配器兼容的函数对象包装器 (函数模板) | 
| 
           
            
             
              (deprecated in C++11)
             
            
            
             
              (removed in C++17)
             
            
           
           | 一元函数指针的适配器兼容包装器 (类模板) | 
| 
           
            
             
              (deprecated in C++11)
             
            
            
             
              (removed in C++17)
             
            
           
           | 适配器兼容的二元函数基类 (类模板) |