所有c++ coder都应该为这个语法感到高兴,说的直白一点,Lambda 表达式就是函数对象的语法糖。
?
?
?
还是直接看对比栗子吧,抄袭的是msdn的官网
?
该示例使用 for_each 函数调用中嵌入的 lambda 向控制台打印 vector 对象中的每个元素是偶数还是奇数。
?
使用lambda
?
复制代码
#include
#include
using namespace std;
?
int main()?
{
? ?// Create a vector object that contains 10 elements.
? ?vector v;
? ?for (int i = 0; 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;
? ? ? }
? ?});
?
? ?// Print the count of even numbers to the console.
? ?cout << "There are " << evenCount?
? ? ? ? << " even numbers in the vector." << endl;
}
复制代码
使用Function Object
?
复制代码
#include
#include
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:
? ? // Default assignment operator to silence warning C4512.
? ? 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 v;
? ? for (int i = 0; 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;
}
复制代码
正如微软文档所言,这两种在效率上并没有实质性的差距,我自己也测试了,不管在debug模式下还是release模式下,果然没有差距。
?
无意中我在晚上发现了bind和Lambda对比测试,前三种方式是网上的,后面两种是我自己加的,结果绝对让我蛋碎了一地。
?
复制代码
#include
#include
#include
#include
#include
#include
?
#if USE_BOOST
#include
#include
#endif
?
?
class FunctorClass
{
public:
? ? // The required constructor for this example.
? ? explicit FunctorClass(uint64_t& 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
? ? {
? ? ? ? ?m_evenCount += n;
? ? }
? ??
private:
? ? // Default assignment operator to silence warning C4512.
? ? FunctorClass& operator=(const FunctorClass&);
? ??
? ? uint64_t& m_evenCount; // the number of even variables in the vector.
};
?
class timer
{
public:
? ? typedef std::chrono::high_resolution_clock clock;
? ? typedef clock::time_point ? ? ? ? ? ? ? ? ?time_point;
? ? typedef clock::duration ? ? ? ? ? ? ? ? ? ?duration;
? ??
public:
? ? timer()
? ? {
? ? ? ? reset();
? ? }
? ??
? ? void reset()
? ? {
? ? ? ? _starttime = clock::now();
? ? }
? ??
? ? duration elapsed() const
? ? {
? ? ? ? return clock::now() - _starttime;
? ? }
prot