简体   繁体   English

初始化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>>

I would like to initialize a boost::shared_ptr<std::vector<std::string> > vec in the constructor initialize list with an boost::shared_ptr<std::list<std::string> > list ? 我想用boost::shared_ptr<std::list<std::string> > list在构造函数初始化列表中初始化boost::shared_ptr<std::vector<std::string> > vec

Is it possible? 可能吗?

I tried this: 我试过这个:

Test.hpp Test.hpp

class Test
{
public:
    Test(boost::shared_ptr<std::list<std::string> > list);

private:
    boost::shared_ptr<std::vector<std::string> > vec;
};

Test.cpp TEST.CPP

Test::Test(boost::shared_ptr<std::list<std::string> > list) : vec(list->begin(), list->end())
{
}

Part of error message: 部分错误消息:

Test.cpp: In constructor ‘Test::Test(boost::shared_ptr<std::list<std::basic_string<char> > >)’:
Test.cpp:6:85: error: no matching function for call to ‘boost::shared_ptr<std::vector<std::basic_string<char> > >::shared_ptr(std::list<std::basic_string<char> >::iterator, std::list<std::basic_string<char> >::iterator)’

Replace: 更换:

vec(list->begin(), list->end())

with: 有:

vec(boost::make_shared(list->begin(), list->end()))

Your constructor should looks like: 您的构造函数应如下所示:

Test::Test(const boost::shared_ptr<std::list<std::string> >& list) :
    vec(boost::make_shared(list->begin(), list->end())){
}

Be aware that you are copying the data from the std::list to the std::vector . 请注意,您正在将数据从std::list复制到std::vector

If you want less expensive solution, you could move them using std::make_move_iterator . 如果您想要更便宜的解决方案,可以使用std::make_move_iterator移动它们。 However, since you still using boost smart pointers, I think that you do not have access to it. 但是,由于您仍然使用boost智能指针,我认为您无法访问它。

Edit: 编辑:

if it did not work try this: 如果它不起作用试试这个:

vec(boost::make_shared<std::vector<std::string>>(list->begin(), list->end()))

Edit 2: 编辑2:

In order to cover the nullptr case as it was mentioned by @Maxim Egorushkin: 为了覆盖@Maxim Egorushkin提到的nullptr案例:

class Test{
public:
    Test(const boost::shared_ptr<std::list<std::string> >& list);

private:
    boost::shared_ptr<std::vector<std::string> > convert_to_vec(const boost::shared_ptr<std::list<std::string> >& lst) const;
    boost::shared_ptr<std::vector<std::string> > vec;
};

//in .cpp
Test::Test(const boost::shared_ptr<std::list<std::string> >& list):
    vec(convert_to_vec(list)){
}
boost::shared_ptr<std::vector<std::string> > Test::convert_to_vec(const boost::shared_ptr<std::list<std::string> >& lst) const{
    if(lst!=nullptr){
        return boost::make_shared<std::vector<std::string>>(list->begin(), list->end());
    }
    return nullptr;
}

As a side note: it is not clear why the constructor takes shared_ptr<X> but does not use the shared_ptr<X> . 作为旁注:不清楚为什么构造函数采用shared_ptr<X>但不使用shared_ptr<X> It should take X& instead. 它应该采取X&而不是。 Avoid using smart-pointers if possible. 尽可能避免使用智能指针。

Ideally, your code should look like: 理想情况下,您的代码应如下所示:

class Test
{
public:
    Test(std::list<std::string> const& list)
        : vec(list.begin(), list.end())
    {}

private:
    std::vector<std::string> vec;
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM