Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating the container of smart pointers by cloning elements of another container

I have a class, which supports cloning (via method clone). I have a bunch of its instances in a vector of std::unique_ptr.

Now, I want to create an std::set of same smart pointers from the above vector, ideally during its construction. The obvious design is as following:

#include <memory>
#include <set>
#include <vector>

class A
{
public:
    /// type of itself
    typedef A self;
    A() = default;
    A(const self& another) = default;
    virtual ~A() = default;

    std::unique_ptr<A> clone() const
    {
        return std::make_unique<A>();
    }
};

class SetOfA
{
public:
    SetOfA() = default;

    // Here is the method I would like to improve
    SetOfA(const std::vector<std::unique_ptr<A> >& data)
    {
        //do not like this loop, prefer this to be in initialization part?
        for (const auto& d : data) {
            set_of_a.insert(std::move(d->clone()));
        }
    }

private:
    std::set<std::unique_ptr <A> > set_of_a;
};

But is there a way to avoid this for loop in constructor and move the std::set construction into initialization part?

like image 484
one_two_three Avatar asked Dec 18 '22 11:12

one_two_three


2 Answers

If you can use Boost, here is a possible solution:

SetOfA(const std::vector<std::unique_ptr<A>>& data) :
    set_of_a(
        boost::make_transform_iterator(data.begin(), std::mem_fn(&A::clone)),
        boost::make_transform_iterator(data.end(),   std::mem_fn(&A::clone)))
    { }
like image 119
Evg Avatar answered Mar 15 '23 23:03

Evg


You could use std::transform to avoid for loop to insert values in a std::set.

SetOfA(const std::vector<std::unique_ptr<A> >&data) 
{
   std::transform(data.begin(), data.end(), std::inserter(set_of_a, set_of_a.begin()), [](auto& a){return a->clone();});
 // or better
 //std::transform(data.begin(), data.end(), std::inserter(set_of_a, set_of_a.begin()), std::mem_fn(&A::clone));
}

It compiles with C++14 and above. Please find tested version of it here https://godbolt.org/z/jzvaKovn7.

And to put the whole stuff in initialization part, you could use a lambda or a Functor, to convert data into set as shown below:

class SetOfA
{
    struct VectorToSetConverter
    {
        std::set<std::unique_ptr<A>> operator()  (const std::vector<std::unique_ptr<A> >&data) const
        {
            std::set<std::unique_ptr<A>> set_of_a;
            std::transform(data.begin(), data.end(), std::inserter(set_of_a, set_of_a.begin()), std::mem_fn(&A::clone));
            return set_of_a;
        }
   };

  public:
  
    SetOfA() = default;
    SetOfA(const std::vector<std::unique_ptr<A> >&data)
    : set_of_a(std::move(VectorToSetConverter()(data))) // move might not be necessary
    {
    }
    
  private:

    std::set<std::unique_ptr <A> > set_of_a;
};

https://godbolt.org/z/6aGPKndb6

like image 43
Sumit Jha Avatar answered Mar 16 '23 00:03

Sumit Jha