使用 for_each 将列表初始化为随机变量

发布于 2024-08-21 01:29:52 字数 267 浏览 4 评论 0原文

我正在尝试使用 for_each 和 lambda 函数将列表初始化为随机整数。我是 boost.lambda 函数的新手,因此我可能会错误地使用它,但以下代码会生成相同数字的列表。每次我运行它时,数字都不同,但列表中的所有内容都是相同的:

srand(time(0));

theList.resize(MaxListSize);

for_each(theList.begin(), theList.end(), _1 = (rand() % MaxSize));

I'm trying to initialize a list to random integers using a for_each and a lambda function. I'm new to boost.lambda functions so I may be using this incorrectly but the following code is producing a list of the same numbers. Every time I run it the number is different but everything in the list is the same:

srand(time(0));

theList.resize(MaxListSize);

for_each(theList.begin(), theList.end(), _1 = (rand() % MaxSize));

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

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

发布评论

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

评论(1

刘备忘录 2024-08-28 01:29:52

Boost lambda 将在函子创建之前评估rand。您需要绑定它,以便在 lambda 求值时对其求值:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp> // for bind
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

int main()
{
    namespace bl = boost::lambda;
    typedef std::vector<int> int_vec;

    static const size_t MaxListSize = 10;
    static const int MaxSize = 20;

    int_vec theList;
    theList.resize(MaxListSize);

    std::srand(static_cast<unsigned>(std::time(0)));
    std::for_each(theList.begin(), theList.end(),
                    bl::_1 = bl::bind(std::rand) % MaxSize);

    std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}

这按预期工作。

然而,正确的解决方案是使用generate_n。为什么要制作一堆 0 来覆盖它们呢?

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

int main()
{
    namespace bl = boost::lambda;
    typedef std::vector<int> int_vec;

    static const size_t MaxListSize = 10;
    static const int MaxSize = 20;

    int_vec theList;
    theList.reserve(MaxListSize); // note, reserve

    std::srand(static_cast<unsigned>(std::time(0)));
    std::generate_n(std::back_inserter(theList), MaxListSize,
                        bl::bind(std::rand) % MaxSize);

    std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}

Boost lambda will evaluate rand before the functor is made. You need to bind it, so it's evaluated at lambda evaluation time:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp> // for bind
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

int main()
{
    namespace bl = boost::lambda;
    typedef std::vector<int> int_vec;

    static const size_t MaxListSize = 10;
    static const int MaxSize = 20;

    int_vec theList;
    theList.resize(MaxListSize);

    std::srand(static_cast<unsigned>(std::time(0)));
    std::for_each(theList.begin(), theList.end(),
                    bl::_1 = bl::bind(std::rand) % MaxSize);

    std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}

This works as expected.

However, the correct solution is to use generate_n. Why make a bunch of 0's just to overwrite them?

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

int main()
{
    namespace bl = boost::lambda;
    typedef std::vector<int> int_vec;

    static const size_t MaxListSize = 10;
    static const int MaxSize = 20;

    int_vec theList;
    theList.reserve(MaxListSize); // note, reserve

    std::srand(static_cast<unsigned>(std::time(0)));
    std::generate_n(std::back_inserter(theList), MaxListSize,
                        bl::bind(std::rand) % MaxSize);

    std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文