函数对象与Lambda

你编写代码时,尤其是使用STL算法时,可能会使用函数指针和函数对象来解决问题和执行计算。函数指针和函数对象各有利弊。例如,函数指针具有最低的语法开销,但不保持范围内的状态,函数对象可保持状态,但需要类定义的语法开销。

lambda结合了函数指针和函数对象的优点并避免其缺点。lambda与函数对象相似的是灵活并且可以保持状态,但不同的是其简洁的语法不需要显式类定义。使用lambda,相比等效的函数对象代码,您可以写出不太复杂并且不容易出错的代码。

下面的示例比较lambda和函数对象的使用。第一个示例使用lambda向控制台打印vector对象中的每个元素是偶数还是奇数。第二个示例使用函数对象来完成相同任务。

示例1:使用lambda

/*
将一个lambda传递给for_each函数,lambda打印一个结果,结果指出vector对象中每个元素是奇数还是偶数
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
   // Create a vector object that contains 10 elements.
   vector<int> v;
   for (int i = 1; i < 10; ++i) {
      v.push_back(i);
   }

   // Count the number of even numbers in the vector by 
   // using the for_each function and a lambda.
   int evenCount = 0;
   for_each(v.begin(), v.end(), [&evenCount] (int n) {
      cout << n;
      if (n % 2 == 0) {
         cout << " is even " << endl;
         ++evenCount;
      } else {
         cout << " is odd " << endl;
      }
   });
   /*
     // for_each 函数

     template <class _InputIterator, class _Function>
     inline _LIBCPP_INLINE_VISIBILITY
     _Function
     for_each(_InputIterator __first, _InputIterator __last, _Function __f)
     {
     for (; __first != __last; ++__first)
     __f(*__first);
     return _VSTD::move(__f);  // explicitly moved for (emulated) C++03
     }
     */
   // Print the count of even numbers to the console.
   cout << "There are " << evenCount 
        << " even numbers in the vector." << endl;
}
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
there are 4 even numbers in the vector.

示例2:使用函数对象
有时lambda过于庞大,无法在上一示例的基础上大幅度扩展。下一示例使用函数对象(而非lambda)以及for_each函数,以产生与示例1相同的结果。两个示例都在vector对象中存储偶数的个数。为保持运算的状态,FunctorClass类通过引用存储 m_evenCount变量作为成员变量。为执行该运算,FunctorClass实现函数调用运算符operator()。Visual C++编译器生成的代码与示例1中的lambda代码在大小和性能上相差无几。对于类似本文中示例的基本问题,较为简单的lambda设计可能优于函数对象设计。但是,如果你认为该功能在将来可能需要重大扩展,则使用函数对象设计,这样代码维护会更简单。

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

class FunctorClass
{
public:
    // The required constructor for this example.
    explicit FunctorClass(int& evenCount)
        : m_evenCount(evenCount) { }

    // The function-call operator prints whether the number is
    // even or odd. If the number is even, this method updates
    // the counter.
    void operator()(int n) const {
        cout << n;

        if (n % 2 == 0) {
            cout << " is even " << endl;
            ++m_evenCount;
        } else {
            cout << " is odd " << endl;
        }
    }

private:

    FunctorClass& operator=(const FunctorClass&);

    int& m_evenCount; // the number of even variables in the vector.
};


int main()
{
    // Create a vector object that contains 10 elements.
    vector<int> v;
    for (int i = 1; i < 10; ++i) {
        v.push_back(i);
    }

    // Count the number of even numbers in the vector by 
    // using the for_each function and a function object.
    int evenCount = 0;
    for_each(v.begin(), v.end(), FunctorClass(evenCount));

    // Print the count of even numbers to the console.
    cout << "There are " << evenCount
        << " even numbers in the vector." << endl;
}

补充:explicit
声明为explicit的构造函数不能在隐式转换中使用。可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生。

class Test1
{
public:
    Test1(int n)
    {
        num=n;
    }//普通构造函数
private:
    int num;
};
class Test2
{
public:
    explicit Test2(int n)
    {
        num=n;
    }//explicit(显式)构造函数
private:
    int num;
};
int main()
{
    Test1 t1=12;//隐式调用其构造函数,成功
    Test2 t2=12;//编译错误,不能隐式调用其构造函数
    Test2 t2(12);//显式调用成功
    return 0;
}

Test1的构造函数带一个int型的参数,代码23行会隐式转换成调用Test1的这个构造函数。而Test2的构造函数被声明为explicit(显式),这表示不能通过隐式转换来调用这个构造函数,因此代码24行会出现编译错误。

普通构造函数能够被隐式调用。而explicit构造函数只能被显式调用。

原文地址:
https://msdn.microsoft.com/zh-cn/library/dd293603.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值