STL 多少就太多了?

发布于 2024-07-16 23:30:54 字数 1437 浏览 5 评论 0原文

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

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

发布评论

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

评论(9

幻想少年梦 2024-07-23 23:30:54

boost::bind 与 std::for_each 一起使用以干净的方式解决这个问题。 或者您可以使用 BOOST_FOREACH

std::for_each 示例:

std::for_each(v.begin(), v.end(), boost::bind(&C::f, _1, param));

BOOST_FOREACH 示例:

std::list<int> list_int( /*...*/ );
BOOST_FOREACH( int i, list_int )
{
    // do something with i
}

Using boost::bind with std::for_each solves this problem in a clean way. Or you can use BOOST_FOREACH.

Example of std::for_each:

std::for_each(v.begin(), v.end(), boost::bind(&C::f, _1, param));

Example of BOOST_FOREACH:

std::list<int> list_int( /*...*/ );
BOOST_FOREACH( int i, list_int )
{
    // do something with i
}
纸短情长 2024-07-23 23:30:54

但它也可以走向相反的方向。 假设您从一个只需要几行的操作开始。 您不想费心创建一个只被调用一次的函数,只是为了压缩循环,所以您编写如下内容:

for ()
{
    // do
    // some
    // stuff
}

然后,随着您需要执行的操作变得更加复杂,您意识到将其拉入一个单独的函数是有道理的,所以你最终会得到

for ()
    do_alot_more_stuff();

然后将其修改为像你原来的方法一样,进一步压缩它是有意义的:

std::for_each(begin, end, do_alot_more_stuff);

最后,将 for_each 真正更改为 for 循环有多难,反之亦然? 不要因为微小的细节而自责!

It can go the opposite way too though. Suppose you start out with an operation that only takes a couple of lines. You don't want to bother creating a function that will only be called once, just to condense the loop, so you write something like:

for ()
{
    // do
    // some
    // stuff
}

Then as the operation you need to perform gets more complex, you realize that pulling it into a separate function makes sense so you end up with

for ()
    do_alot_more_stuff();

And then modifying it to be like your original method makes sense to condense it down further:

std::for_each(begin, end, do_alot_more_stuff);

In the end, how hard is it to really change a for_each to a for loop, or vice versa? Don't beat yourself up over tiny details!

吝吻 2024-07-23 23:30:54

像任何其他语言工具一样使用它。 当它让你的生活更轻松时,就使用它。 当事情变得麻烦时,就做点别的事。 当需求发生变化时,以某种方式重构循环并不是真的很难。

Use it like any other language tool. When it makes your life easier, use it. When it becomes cumbersome, do something else. It's not as if it's really hard to refactor a loop one way or another, when requirements change.

混浊又暗下来 2024-07-23 23:30:54

与您的问题类似,我经常注意到 C++ 中的“函子”模式/习惯用法实际上非常笨重。 这就是为什么我期待 Lambda 函数 在 C++0X 中。 其中一些现在可以通过 boost::lambda 实现。

Similar to your issue, I often notice that the "functor" pattern / idiom in C++ is actually quite unwieldy. That's why I'm looking forward to Lambda Functions in C++0X. Some of that is possible with boost::lambda now.

满栀 2024-07-23 23:30:54

我在算法中的很多内容上都遇到了同样的问题。 它有一个令人讨厌的倾向,即最终成为仅使用老式 for 循环的更多代码。

我不能真正证明使用适当的构造函数和析构函数,也许还有一些访问器来创建一些特殊的仿函数类(这是 C++ 中的一个相当高级的主题,我的许多维护者不会完全理解),只是为了避免一行 for 循环。

I've had the same problem with a lot of the stuff in Algorithm. It has a nasty tendency to end up being more code that just using an old-fashioned for loop.

I can't really justify going off and creating some special functor class (which is a moderately advanced topic in C++ that many of my maintainers won't fully understand) with a proper constructor and destructor, and perhaps some accessors, simply to avoid a one-line for loop.

情泪▽动烟 2024-07-23 23:30:54

也许您首先使用了 for_each 而不是转换......

Maybe you used for_each instead of transform in the first place...

薄情伤 2024-07-23 23:30:54

我从不使用 std::for_each (或很少)。

我建议使用 Boost.Foreach 和 classic现在是“for”结构。 当 C++0x 发布时,您可以考虑使用新的“for”构造,使容器迭代更具可读性。

I never use std::for_each (or very seldom).

I'd suggest using Boost.Foreach and classic "for" constructions for now. And when C++0x is out, you could consider using the new "for" construct, made more readable for iterating through containers.

离旧人 2024-07-23 23:30:54

还要考虑并行性,通过一个函数,您可以定义将要更改的内容,并指示是否可以从开始到结束一次并行地完成一系列元素而不是一次。

Also think of Parallelism, with a function you can define what will change and indicate if a range of elements can instead be done in Parallel rather 1 at a time from start to end.

像极了他 2024-07-23 23:30:54

或者您可以等待 C++0x 并使用 for(elem& e, container){e.something();}
BOOST_FOREACH() 完全相同,但是是标准的一部分(在某些年份......)。

Or you could wait for C++0x and use for(elem& e, container){e.something();}
Quite the same as BOOST_FOREACH(), but part of the standard (in some years...).

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