使用共享指针向量增强 lambda

发布于 2024-12-08 16:10:55 字数 1945 浏览 1 评论 0原文

下面是一个稍微修改过的代码,来自一个很好的示例,如何将值从一个字符串向量复制到另一个对象向量。 (请参阅:另一种复制算法

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

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;

class Object
{
public:
    std::string Name;

    Object(const std::string& Name_ = "")
        : Name(Name_)
    {
    }
};

int main()
{
    //std::vector<Object> objects(3, Object());
    std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

    std::vector<std::string> names;
    names.push_back("Alpha");
    names.push_back("Beta");
    names.push_back("Gamma");
    std::vector<std::string>::const_iterator names_it;
    names_it = static_cast<const std::vector<std::string>&>(names).begin();

    //std::for_each(objects.begin(), objects.end(), bind(&Object::Name, _1) = *var(names_it)++);
    std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

    //std::vector<Object>::iterator it, end = objects.end();
    std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
    for (it = objects.begin(); it != end; ++it) {
        //std::cout << it->Name << std::endl;
        std::cout << (*it)->Name << std::endl;
    }

    return EXIT_SUCCESS;
}

在这种情况下,我使用动态分配的对象,因为 boost::lambda:: bind 无法处理像 boost::bind 那样的更改,我需要取消引用占位符才能编译:

std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

但是在输出中我得到了:

Gamma
Gamma
Gamma

你的解释是什么?

Below is a slightly modified code from one good example how to copy values fro one vector of strings to another vector of objects. (see: another copy algorithm )

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

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;

class Object
{
public:
    std::string Name;

    Object(const std::string& Name_ = "")
        : Name(Name_)
    {
    }
};

int main()
{
    //std::vector<Object> objects(3, Object());
    std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

    std::vector<std::string> names;
    names.push_back("Alpha");
    names.push_back("Beta");
    names.push_back("Gamma");
    std::vector<std::string>::const_iterator names_it;
    names_it = static_cast<const std::vector<std::string>&>(names).begin();

    //std::for_each(objects.begin(), objects.end(), bind(&Object::Name, _1) = *var(names_it)++);
    std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

    //std::vector<Object>::iterator it, end = objects.end();
    std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
    for (it = objects.begin(); it != end; ++it) {
        //std::cout << it->Name << std::endl;
        std::cout << (*it)->Name << std::endl;
    }

    return EXIT_SUCCESS;
}

In this case I'm using dynamically allocated Objects, and because boost::lambda::bind can't handle such changes as boost::bind do, I need to dereference placeholder in order to compile:

std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

But then at the output I've got:

Gamma
Gamma
Gamma

What is your explanation?

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

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

发布评论

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

评论(1

我的奇迹 2024-12-15 16:10:55
std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

这相当于:

shared_ptr<Object> object = make_shared<Object>();
std::vector<shared_ptr<Object> > objects(3, object);

向量构造函数将创建该对象指针的 3 个副本,它们都将引用同一个且唯一< code>Object(即 *object)。用单独的指针填充向量,每个指针都用自己的对象初始化。

std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

This is equivalent to doing:

shared_ptr<Object> object = make_shared<Object>();
std::vector<shared_ptr<Object> > objects(3, object);

The vector constructor will then make 3 copies of that object pointer, which will all refer to same one and unique Object (that is to say, *object). Fill the vector with separate pointers each initialized with their own Object.

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