std::move_iterator in C++ 11
Last Updated :
05 Oct, 2023
std::move_iterator was introduced in C++11, with the ability to convert regular iterators into move iterators. Instead of copying, the std::move_iterator allows the STL to move the objects it manipulates. The move iterator wraps the other iterator. This is particularly helpful while working with huge information designs or items that can be moved yet not duplicated effectively.
Syntax
To create a move iterator from the existing container's iterator, we use the make_move_iterator() function as shown:
move_iterator <container<T>::iterator> itr = make_move_iterator( container<T>:::iterator );
Here,
- <container<T>:::iterator>: It specifies the template argument for move_iterator. In this case, it specifies the type of the iterator that my_move_iterator will wrap that can iterate over elements of a vector.
- make_move_iterator: A function template that constructs a std::move_iterator for the given iterator.
Example 1: Moving All Elements From the Source to the Destination
BEFORE:
Source Vector = {1, 2, 3, 4, 5}
Destination Vector = {}
AFTER:
Source Vector = {}
Destination Vector = {1, 2, 3, 4, 5}
Example 2: Moving Elements From the Middle of the Source Vector
BEFORE:
Source Vector = {1, 2, 3, 4, 5}
Destination Vector = {}
AFTER:
Source Vector = {1, 2}
Destination Vector = {3, 4, 5}
C++ Program to Implement std::move_iterator
Let's explore a basic example to understand how std::move_iterator works:
C++
// C++ Program to implement move_iterator
#include <bits/stdc++.h>
using namespace std;
int main()
{
// source vector
vector<string> v1 = { "Hi", "Geeks", "for", "Geeks" };
// destination vector
vector<string> v2;
cout << "Before move iterator\n";
cout << "Source: ";
for (auto s : v1) {
cout << s << ' ';
}
cout << endl;
cout << "Destination: ";
for (auto s : v2) {
cout << s << ' ';
}
cout << endl;
// using std::copy to copy elements from one vector to
// another
copy(make_move_iterator(begin(v1)),
make_move_iterator(end(v1)), back_inserter(v2));
cout << "\nAfter move iterator\n";
cout << "Source: ";
for (auto s : v1) {
cout << s << ' ';
}
cout << endl;
cout << "Destination: ";
for (auto s : v2) {
cout << s << ' ';
}
cout << endl;
return 0;
}
OutputBefore move iterator
Source: Hi Geeks for Geeks
Destination:
After move iterator
Source:
Destination: Hi Geeks for Geeks
In this code:
- We create a vector<string> v1 with some initial values.
- A new vector v2 is created to store the moved elements.
- We use std::copy to efficiently transfer ownership of elements from v1 to v2 using move iterators.
- After the move, v1 is empty because ownership of its elements has been transferred.
- Finally, we print the contents of both vectors to verify the result.
When to Use std::move_iterator?
You should consider using std::move_iterator in the following scenarios:
- Optimizing Memory Usage: When you need to transfer elements from one container to another while minimizing memory overhead.
- Performance Enhancement: When you want to improve performance by avoiding unnecessary copying of elements.
- Containers with Move Semantics: When you're working with containers, like std::vector, that support move semantics.C++ Program to Use std::move_iterator.
Similar Reads
Iterators in C++ STL An iterator in C++ is a pointer-like object that points to an element of the STL container. They are generally used to loop through the contents of the STL container in C++. The main advantage of STL iterators is that they make the STL algorithms independent of the type of container used. We can jus
10 min read
Output Iterators in C++ After going through the template definition of various STL algorithms like std::copy, std::move, std::transform, you must have found their template definition consisting of objects of type Output Iterator. So what are they and why are they used ?Output iterators are one of the five main types of ite
6 min read
Iterators Operations in C++ Iterators are pointer like objects that are used to refer iterator the STL containers. Just like pointer arithmetic, there are some operations that are allowed on iterators in C++. They are used to provide different functionalities to make iterator more powerful and versatile. In this article, we wi
9 min read
std::move in C++ std :: move Moves the elements in the range [first,last] into the range beginning at the result. The value of the elements in the [first,last] is transferred to the elements pointed out by the result. After the call, the elements in the range [first,last] are left in an unspecified but valid state.
2 min read
std::inserter in C++ std::inserter constructs an insert iterator that inserts new elements into x in successive locations starting at the position pointed by it. It is defined inside the header file . An insert iterator is a special type of output iterator designed to allow algorithms that usually overwrite elements (su
4 min read
regex_iterator() function in C++ STL regex_iterator() is a function from the BiDirectionalIterator class in C++. This method returns an iterator type to iterate over different matches of a same regex pattern in a sequence. Syntax: template< class BidirectionalIterator, class CharT = typename std::iterator_traits::value_type, class T
2 min read