本文介绍了c ++ boost lambda库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

保留在C ++语言的边界之内和库,我建议先习惯使用STL算法函数模板进行编程,因为boost :: lambda最常见的用法是用内联表达式替换函子类。



库文档本身为您提供了一个前台示例:

  for_each a.begin(),a.end(),std :: cout<<<<<<<'';; 

其中 std :: cout<< _1<< ''产生一个函数对象,当被调用时,它的第一个参数写入 cout 流中。这是你可以用一个自定义函子类来做的事情, std :: ostream_iterator 或者一个显式循环,但是boost :: lambda可以简洁并且可能很清晰 - 至少如果你已经习惯了函数式编程概念。当你(使用STL)时,你会发现你对boost :: bind和boost :: lambda感兴趣。它很适合用于以下方面:

  std :: sort(c.begin(),c.end(), bind(& Foo :: x,_1)< bind(& Foo :: x,_2)); 

在你达到那一点之前,不要太多。因此,使用STL算法,编写自己的函数,然后使用boost :: lambda将它们转换为内联表达式。

从专业的角度来看,我相信开始的最佳方法使用boost :: lambda来获取boost :: bind的理解和接受。在boost :: bind表达式中占位符的使用与裸boost :: lambda占位符相比看起来更不可思议,并且在代码审阅期间更容易被接受。超越基本的boost :: lambda使用很可能会让你的同事感到悲伤,除非你是在一个流血的C ++商店。



尽量不要过分 - -loop 真的是正确的解决方案。


What might be the best way to start programming using boost lambda libraries.

解决方案

Remaining within the boundaries of the C++ language and libraries, I would suggest first getting used to programming using STL algorithm function templates, as one the most common use you will have for boost::lambda is to replace functor classes with inlined expressions inlined.

The library documentation itself gives you an up-front example of what it is there for:

for_each(a.begin(), a.end(), std::cout << _1 << ' ');

where std::cout << _1 << ' ' produces a function object that, when called, writes its first argument to the cout stream. This is something you could do with a custom functor class, std::ostream_iterator or an explicit loop, but boost::lambda wins in conciseness and probably clarity -- at least if you are used to the functional programming concepts.

When you (over-)use the STL, you find yourself gravitating towards boost::bind and boost::lambda. It comes in really handy for things like:

std::sort( c.begin(), c.end(), bind(&Foo::x, _1) < bind(&Foo::x, _2) );

Before you get to that point, not so much. So use STL algorithms, write your own functors and then translate them into inline expressions using boost::lambda.

From a professional standpoint, I believe the best way to get started with boost::lambda is to get usage of boost::bind understood and accepted. Use of placeholders in a boost::bind expression looks much less magical than "naked" boost::lambda placeholders and finds easier acceptance during code reviews. Going beyond basic boost::lambda use is quite likely to get you grief from your coworkers unless you are in a bleeding-edge C++ shop.

Try not to go overboard - there are times when and places where a for-loop really is the right solution.

这篇关于c ++ boost lambda库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:54