How do I move a unique pointer from one vector to another vector of unique pointers?

J Doe

How do I move a unique_ptr from one vector to another vector of unique_ptrs in C++11? The unique pointer from the first vector should be completely removed and added to the second vector.

Deduplicator

Well, in that case you have two conceptually independent operations:

  1. Inserting an element into a container. As you want to obliterate the source (which is actually necessary because std::unique_ptr is a move-only type), use std::move to enable move-semantics.

    destination.emplace(destination.begin() + m, std::move(source[n])); // or .insert()
    
  2. Removing the plundered element from the container.

    source.erase(source.begin() + n);
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to construct a vector with unique pointers

Vector of unique pointers: Finding and then rotating an element to the front of a vector by pointer

How do I find unique sequences in a vector?

How to turn vector of raw pointers into a vector of unique pointers?

Unique pointer to vector and polymorphism

How to return an array / vector of unique pointers?

how to change the visibility of a vector of unique pointers

How do I move a vector of objects that contain a unique_ptr as a member?

Filling of vector with unique_pointers

Clone vector of unique pointers with RTTI

Using a vector of unique pointers to an <Employee> vector

How do you move a unique_ptr out of an vector<unique_ptr<Foo>>?

How to move an object from one vector to another, without destroying any?

How do I assign an object containing an unique_ptr to a vector of its type when a move assignment operator is defined?

Using raw pointers from vector of unique_ptr as an ID for sorting

How to extract elements from vector using another vector with non unique values?

How to calculate the number of unique numbers in one vector by different values from other vector

How to use std::find_if with a vector of unique pointers?

How to initialize a vector of unique_ptr with null pointers?

How to use a vector of unique pointers in a dll exported class with Visual Studio

How to create a vector of unique pointers pointing at default constructed objects

How to model a vector of non-owning raw pointers extracted from unique_ptrs?

How do I write a function to convert a character vector into a character vector of unique pairs of its elements?

How do I copy one vector to another vector in Java without updating the original vector?

how to remove unique values from a vector

Inserting a vector of unique_ptr into another vector

How to move one vector into another vector without copying

How to pass a pointer to vector of pointers?

Visual Studio: Vector of Unique Pointers of Subclasses Crashes