C++STL之函数对象及谓词


概述
函数对象是重载()运算符的类类型对象,即该类实现operator()()函数。STL将一组标准的函数对象定义为模板,,可以使用它们来创建一个函数对象,其中overload()运算符函数使用我们的对象类型。如:STL定义模板less<T>。如果将该模板实例化为less<myNumber>,我们就有了一个函数对象类型,实现operator()()来对myNumber类型的对象进行大小比较。


很多算法使用函数对象来指定要进行的二元运算,或者指定谓词来确定进行特定运算。谓词是一个返回bool类型的值的函数,因为函数对象是一种类型的对象,实现operator()()成员函数并返回bool类型的值,所以函数对象也是谓词。


谓词有两个版本,分别是需要两个操作数的二元谓词和需要一个操作数的一元谓词。后文将会看到具体的应用。


应用
list中的remove_if()函数基于应用一元谓词的结果来删除列表中的元素,它返回一个bool型的值true或false。如果向一个元素应用谓词的值为true,就会从列表中删除该元素。通常我们会自己来定义自己的谓词做我们想做的事情。这就需要为函数对象定义想采用的自己的类模板,STL定义用在这种上下文中的unary_function(T,R)基础模板,定义如下:




template < class _Arg, class _Result > 
struct unary_function
{
    // base class for unary function
    typedef _Arg argument_type;
    typedef _Result result_type;
};


用个具体的实例来展示用法。




//functionObject.h
#pragma once
#include <functional>


template <class T> class is_negative: public std::unary_function<T, bool>
{
public:
    result_type operator()(T& value)
    {
        return value < 0;
    }
};


template < class T> 
class is_large: public std::unary_function<T,bool>
{
public:
    result_type operator()(T &value)
    {
        //return value > 10;
        if (value > 10)
        {
            return true;
        }
        else
            return false;
    }
};


template < class T> 
class is_odd : public std::unary_function<T,bool>
{
public:
    result_type operator()(argument_type &value)
    {
        if ((value % 2) == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
};




#include <iostream>
#include <list>
#include "function_object.h"
#include <functional>


using std::cin;
using std::cout;
using std::endl;
using std::list;
using std::greater;


// Template function to list the contents of a list
template <class T>
void listlist(list<T>& data)
{
    for(list<T>::iterator iter = data.begin() ; iter != data.end() ; iter++)
        cout << *iter << "  ";
    cout << endl;
}


// Template function to read data from cin and store it in a list
template<class T>
void loadlist(list<T>& data)
{
    T value = T();
    while(cin >> value , value != T())  //Read non-zero values
        data.push_back(value);
}


int main()
{
    // Process integers
    list<int> numbers;
    cout << "Enter non-zero integers separated by spaces. Enter 0 to end." 
        << endl;
    loadlist(numbers);
    cout << "The list contains:" << endl;
    listlist(numbers);


    numbers.remove_if(is_odd<int>());
    cout << "After applying the remove_if(is_odd()) function the list contains:" 
        << endl;
    listlist(numbers);


    numbers.remove_if(is_negative<int>()); 
    cout << "After applying the remove_if() function the list contains:" 
        << endl;
    listlist(numbers);


    // Process floating-point values
    list<double> values;
    cout << endl 
        << "Enter non-zero values separated by spaces. Enter 0 to end." 
        << endl;
    loadlist(values);
    cout << "The list contains:" << endl;
    listlist(values);
    
    values.remove_if(is_negative<double>());        
    cout << "After applying the remove_if() function the list contains:" << endl;
    listlist(values);


    // Another list to use in merge
    list<double> morevalues;
    cout << endl 
        << "Enter non-zero values separated by spaces. Enter 0 to end." 
        << endl;
    loadlist(morevalues);
    cout << "The list contains:" << endl;
    listlist(morevalues);
    values.remove_if(is_negative<double>());        
    cout << "After applying the remove_if() function the list contains:" << endl;
    listlist(morevalues);


    // Merge the last two lists
    values.sort(greater<double>());
    morevalues.sort(greater<double>());
    values.merge(morevalues, greater<double>());
    listlist(values);


    return 0;
}


Functional头文件定义了一个可扩展的模板集合,用来创建可用于算法和容器的函数对象。常见的有:


全部用于创建二元谓词


函数模板对象


说明


less<T>


表示T类型的<运算。如less<string>()定义一个比较string类型的函数对象。


less_equal<T>


同上,表示<=运算。


equal<T>


同上,表示=运算。


not_equal<T>


同上,表示!=运算。


greater_equal<T>


同上,表示>=运算。


greater<T>


同上,表示>运算。


not2<B>


它是B类型的二元谓词的负值。


 


二元谓词的创建基本同一元谓词,但是用不同的模板而已。




template<class _Arg1,
class _Arg2,
class _Result>
struct binary_function
{    // base class for binary functions
    typedef _Arg1 first_argument_type;
    typedef _Arg2 second_argument_type;
    typedef _Result result_type;
};


例如:




class personValue:binary_function<Person, Person,bool>
{
    result_type operator()(const first_argument_type &p1,
                           const second_argument_type &p2) const
    {
        return p1 > p2;


    }
};


实际运用:


主要应用于优先级队列,简单介绍优先级队列。默认情况下,优先级队列适配器类使用的基础容器为vector<T>。可以选择不同的序列容器作为基础,并选择一个备用的函数对象确定元素的优先级。如:


priority_queue<int,vector<int>,greater<int> > numbers;
这条语句基于vector<int>容器定义优先级队列,用greater<int>类型的函数对象插入元素。这个优先级队列中的元素将以降序排列,顶部为最小的元素。3 个模板形参是元素类型、要作为基础的容器、要用来排列元素顺序的谓词类型。


如想想应用默认的谓词(在这种情况下将是less<T>),则可以省略第三个参数。否则必须显示地定义。


实例:




include <iostream>
#include <vector>
#include <queue>
#include <functional>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::priority_queue;
using std::greater;
using std::binary_function;
class com:binary_function<int,int,bool>
{
public:
    result_type operator()(first_argument_type &p1,
         second_argument_type &p2) const
    {
        return p1>p2;
    }
};
//class com
//{
//public:
//    bool operator()(int &p1,int &p2)const
//    {
//        return p1<p2;
//    }
//};
int main()
{
    priority_queue<int,vector<int>,com > numbers;
    int num;
    cout<<"input numbers 0 to end"<<endl;
    while(1)
    {
        cin>>num;
        if (0 == num)
        {
            break;
        }
        numbers.push(num);
    }
    cout << endl << "There are " << numbers.size()
        << " numbers in the queue." 
        << endl << endl;
    while(!(numbers.empty()))
    {
        cout<<numbers.top()<<"  ";
        numbers.pop();
    }
    return 0;
}


我们将第三个参数的模板类型改成了我们自己的函数对象。可以通过继承binary_function()或者自己定义(如注释部分),效果是同样的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值