c++ 增强 lambda 库

发布于 2024-07-06 07:08:28 字数 39 浏览 6 评论 0原文

使用 boost lambda 库开始编程的最佳方法可能是什么。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮凉 2024-07-13 07:08:28

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

库文档本身为您提供了一个预先示例,说明它的用途:

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

其中 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++ 商店。

尽量不要太过分 - 在某些时候和某些地方,for-循环确实是正确的解决方案。

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.

脱离于你 2024-07-13 07:08:28

依靠。 您是否已经精通函数式编程概念? 如果没有,我建议您从专为函数式编程设计的语言开始,而不是使用带有函数功能的过程语言。 如果您不习惯以函数式风格进行编码(它不一定更难,但它绝对不同),那么您将花费更多的时间来对抗语法,而没有足够的时间学习来完成工作。

至于从哪里开始,我在Scheme上初步了解了功能,但还有很多不错的选择。

Depends. Are you already well versed in functional programming concepts? If not, I'd suggest that you start in a language that is designed for functional programming instead of a procedural language with functional features stapled on. If you aren't used to coding in a functional style (it's not harder necessarily, but it's definitely different), then you'll spend more time fighting the syntax and not enough time learning to get stuff done.

As for where to start, I cut my functional teeth on Scheme, but there are plenty of good options.

栖迟 2024-07-13 07:08:28

如果您使用的是相当新的编译器,则可以使用 boost。 如果您的计算机上尚未安装它,请安装它(在 unbuntu 上执行 sudo apt-get install libboost-dev,如果您使用的是 Windows,请从 boost.org 获取二进制文件)。 阅读 doc,然后查看您现有的为您可能使用它们的情况编写代码。 例如,如果您使用一小段代码对函数进行参数化,是否可以消除大量重复代码?

If you are working with a reasonably recent compiler, you can use boost. If it's not on your machine already, install it (sudo apt-get install libboost-dev on unbuntu, get the binaries from boost.org if you are on windows). Read the doc's then look at your existing code for situations where you might use them. Do you have a lot of code duplication that could be eliminated if you parametrized a function with a small piece of code, for example?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文