c ++ STL objects of built-in functions

Some objects built STL functions: arithmetic functor relationship functor logic functor

usage:

These objects functor generated, and general usage functions are identical;

Use the built-in function objects need to include headers # <include> <functional>

First, the arithmetic functor

#include <the iostream>
 the using  namespace STD; 
#include <Functional> // built-in function object header file 

// built-in arithmetic function object functor 

// o negate one yuan negated functor functor 
void Test01 () 
{ 
    o negate < int > n-; 

    COUT << n-( 50 ) << endl; 
} 

// PLUS binary functor adder 
void Test02 () 
{ 
    PLUS < int > P; 

    COUT << P ( 10 , 20 is ) << endl; 
} 

int main() {

    test01();

    test02();

    system("pause");

    return 0;
}

Second, the relationship between functor

#include <the iostream>
 the using  namespace STD; 
#include <Vector> 
#include <algorithm> 
#include <Functional> // built-in function object relationship functor _  
 // greater than Greater class MyCompare 
{ public :
     BOOL operator () ( int V1 , int V2) 
    { return V1> V2; 
    } 
}; void Test01 () 
{ 
    Vector < int > V; 
    v.push_back ( 10 );




 
        


 
    v.push_back (30);
    v.push_back(40);
    v.push_back(20);
    v.push_back(50);

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //降序
    //sort(v.begin(), v.end(), MyCompare());
    //greater<int>() 内建函数对象
    sort(v.begin(), v.end(), greater<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

}

int main() {

    test01();

    system("pause");

    return 0;
}

3. Logic functor

#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>

//内建函数对象_逻辑仿函数
//逻辑非  logical_not

void test01()
{
    vector<bool>v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);

    for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
    {
        COUT << * IT << "  " ; 
    } 
    COUT << endl; 

    // use the logic NOT transport container into the container v2, v, and perform negation operation 
    Vector < BOOL > v2; 
    v2.resize (v.size () ); 

    Transform (v.begin (), v.end (), v2.begin (), logical_not < BOOL > ()); 

    for (Vector < BOOL > :: = v2.begin IT Iterator (); IT =! v2.end (); IT ++ ) 
    { 
        COUT << * IT << "  " ; 
    } 
    COUT << endl;

}


int main() {
 
    Test01 ();

    system("pause");

    return 0;
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12112483.html