简体   繁体   English

如何复制std :: vector <boost::shared_ptr<T> &gt;到std :: list <T>

[英]How to copy std::vector<boost::shared_ptr<T>> to std::list<T>

std::list<KinBody::Link::Geometry> geometries = link->GetGeometries();

The type of link->GetGeometries() is std::vector<boost::shared_ptr<Geometry>> and I got the following error with the above code. link->GetGeometries()的类型为std::vector<boost::shared_ptr<Geometry>>并且上面的代码出现以下错误。

error: conversion from ‘const std::vector<boost::shared_ptr<OpenRAVE::KinBody::Link::Geometry> >’ to     non-scalar type ‘std::list<OpenRAVE::KinBody::Link::Geometry>’ requested
std::list<KinBody::Link::Geometry> geometries = link->GetGeometries();

What should I do ? 我该怎么办 ?

std::list<KinBody::Link::Geometry> geometries;

for (auto const & p : link->GetGeometries())
    geometries.push_back(*p);

For the for (auto const & p : ...) part you need C++11 support enabled (it uses automatic type deduction and range-based for loop). 对于for (auto const & p : ...)部分,您需要启用C ++ 11支持(它使用自动类型推导和基于范围的for循环)。

Pre-C++11 equivalent is C ++ 11之前的版本是

std::list<KinBody::Link::Geometry> geometries;

typedef std::vector<boost::shared_ptr<KinBody::Link::Geometry>> geometries_vector_t;

geometries_vector_t const & g = link->GetGeometries();

for (geometries_vector_t::const_iterator i = g.begin(); i != g.end(); ++i)
    geometries.push_back(**i); // dereferencing twice: once for iterator, once for pointer

NB: all this looks very unnatural. 注意:所有这些看起来非常不自然。 Objects being returned as shared pointers means that KinBody::Link::Geometry is actually a base class or interface, or that objects of this type are large and the interface is designed to avoid heavy copying, or something else. 作为共享指针返回的对象意味着KinBody::Link::Geometry实际上是基类或接口,或者该类型的对象很大,并且该接口旨在避免大量复制或其他操作。 I advise not to copy the objects and store them as shared pointers as the interface suggests, unless you actually know you need the copies. 我建议不要复制对象,也不要像接口所建议的那样将它们存储为共享指针,除非您实际上知道需要复制。

Since you mentioned boost, let me show you some Boost Range sugar. 既然您提到了增强功能,那么让我向您展示一些增强范围糖。

link->getgeometries() | adaptors::indirected

this will result in a range that contains the Geometry& elements. 这将导致包含Geometry&元素的范围。 Fill the list with copy_range : copy_range填充列表:

geometries = boost::copy_range<std::list<link::geometry>>(
        link->getgeometries() | adaptors::indirected
    );

See a fully working demo: 观看完整的演示:

Live On Coliru 生活在Coliru

#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/make_shared.hpp>
#include <iostream>

namespace KinBody {

    struct Link {
        struct Geometry {};

        std::vector<boost::shared_ptr<Geometry> > GetGeometries() const {
            return {};
        }
    };
}

int main() {
    using namespace boost;
    using namespace KinBody;

    auto link = make_shared<Link>();
    auto geometries = boost::copy_range<std::list<Link::Geometry>>(
            link->GetGeometries() | adaptors::indirected
        );
}

The line 线

std::list<KinBody::Link::Geometry> geometries = link->GetGeometries();

tries to assign a vector to a list. 尝试将向量分配给列表。 The compiler tells you that you cannot do that. 编译器告诉您您不能这样做。 What you have to do is iterate the vector and insert each element to the list. 您要做的是迭代向量并将每个元素插入列表。

Note that the vector contains shared pointers to geometries whereas the list is declared to contain instances. 请注意,向量包含指向几何的共享指针,而列表被声明为包含实例。 If you want to store a pointer in the list you have to declare it as: 如果要将指针存储在列表中,则必须将其声明为:

std::list<KinBody::Link::Geometry*>  // note the *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化boost :: shared_ptr <std::vector<T> &gt;使用boost :: shared_ptr <std::list<T> &gt; - Initializing boost::shared_ptr<std::vector<T>> with boost::shared_ptr<std::list<T>> std :: stack &lt;boost :: shared_ptr的深层副本 <T> &gt; - Deep copy of std::stack< boost::shared_ptr<T> > 迭代shared_ptr <std::vector<T> &gt; - Iterate shared_ptr<std::vector<T>> 转换std :: vector的绝佳解决方案 <boost::shared_ptr<T> &gt;进入std :: vector <T*> - Elegant solution to converting a std::vector<boost::shared_ptr<T>> into a std::vector<T*> 移动std :: vector <std::unique_ptr<T> &gt;到std :: vector <std::shared_ptr<T> &gt; - Move std::vector<std::unique_ptr<T>> to std::vector<std::shared_ptr<T>> 正在使用 std::vector&lt; std::shared_ptr<const T> &gt; 反模式? - Is using std::vector< std::shared_ptr<const T> > an antipattern? 为什么我不能在C ++ 0x中的std :: shared_ptr的向量上执行std :: copy? - Why can't I perform a std::copy on a vector of std::shared_ptr's in C++0x? 转换 const std::shared_ptr<const t> 进入 boost::shared_ptr<t></t></const> - convert const std::shared_ptr<const T> into boost::shared_ptr<T> 转换表单boost :: shared_ptr时出错 <T> 到std :: shared_ptr <T> - Error converting form boost::shared_ptr<T> to std::shared_ptr<T> boost :: ptr_container和std :: vector <shared_ptr> - boost::ptr_container and std::vector<shared_ptr>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM