Skip to content

Latest commit

 

History

History
112 lines (96 loc) · 2.6 KB

File metadata and controls

112 lines (96 loc) · 2.6 KB
title unary_function Struct | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-standard-libraries
ms.tgt_pltfrm
ms.topic article
f1_keywords
unary
functional/std::unary
dev_langs
C++
helpviewer_keywords
unary_function class
ms.assetid 04c2fbdc-c1f6-48ed-b6cc-292a6d484627
caps.latest.revision 21
author corob-msft
ms.author corob
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

unary_function Struct

An empty base struct that defines types that may be inherited by derived classes that provides a unary function object.

Syntax

struct unary_function 
{
   typedef Arg argument_type;
   typedef Result result_type;
};  

Remarks

The template struct serves as a base for classes that define a member function of the form result_typeoperator()( constargument_type&) const.

All such derived unary functions can refer to their sole argument type as argument_type and their return type as result_type.

Example

// functional_unary_function.cpp  
// compile with: /EHsc  
#include <vector>  
#include <functional>  
#include <algorithm>  
#include <iostream>  
  
using namespace std;  
  
// Creation of a user-defined function object  
// that inherits from the unary_function base class  
class greaterthan10: unary_function<int, bool>  
{  
public:  
    result_type operator()(argument_type i)  
    {  
        return (result_type)(i > 10);  
    }  
};  
  
int main()  
{  
    vector<int> v1;  
    vector<int>::iterator Iter;  
  
    int i;  
    for (i = 0; i <= 5; i++)  
    {  
        v1.push_back(5 * i);  
    }  
  
    cout << "The vector v1 = ( " ;  
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)  
        cout << *Iter << " ";  
    cout << ")" << endl;  
  
    vector<int>::iterator::difference_type result1;  
    result1 = count_if(v1.begin(), v1.end(), greaterthan10());  
    cout << "The number of elements in v1 greater than 10 is: "  
         << result1 << "." << endl;  
}  
\* Output:   
The vector v1 = ( 0 5 10 15 20 25 )  
The number of elements in v1 greater than 10 is: 3.  
*\  

Requirements

Header: <functional>

Namespace: std

See Also

Thread Safety in the C++ Standard Library
C++ Standard Library Reference