std::move_iterator, introduced in C++11, is an iterator adapter that enables STL algorithms to move elements instead of copying them. It is particularly useful for improving performance when working with movable objects.
- Converts ordinary iterators to support move semantics.
- Helps reduce the overhead of unnecessary copying.
Example: Moving Elements Between Containers
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
vector<string> source = {
"Hi", "Geeks", "for", "Geeks"
};
vector<string> destination;
cout << "Before move:\n";
cout << "Source: ";
for (auto& s : source)
cout << s << " ";
cout << "\nDestination: ";
for (auto& s : destination)
cout << s << " ";
copy(make_move_iterator(source.begin()),
make_move_iterator(source.end()),
back_inserter(destination));
cout << "\n\nAfter move:\n";
cout << "Source: ";
for (auto& s : source)
cout << s << " ";
cout << "\nDestination: ";
for (auto& s : destination)
cout << s << " ";
return 0;
}
Output
Before move: Source: Hi Geeks for Geeks Destination: After move: Source: Destination: Hi Geeks for Geeks
Explanation
- make_move_iterator() converts the normal iterators of source into move iterators.
- std::copy() then moves the elements instead of copying them.
- After the move operation, the elements in the source container remain in a valid but unspecified state.
Syntax
A move iterator can be created using the std::make_move_iterator() helper function:
auto itr = make_move_iterator(container.begin());
It can also be created directly:
std::move_iterator<Container::iterator> itr(container.begin());
Example: Moving a Subrange of Elements
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
vector<int> source = {1, 2, 3, 4, 5};
vector<int> destination;
copy(make_move_iterator(source.begin() + 2),
make_move_iterator(source.end()),
back_inserter(destination));
for (int x : destination)
cout << x << " ";
}
Output
3 4 5
Explanation: Only the elements from index 2 onward are moved into the destination container.
Advantages of std::move_iterator
std::move_iterator provides several advantages:
- Avoids unnecessary copying of objects.
- Improves performance for large containers.
- Enables STL algorithms to use move semantics.
- Works seamlessly with movable types such as std::string, std::vector, and user-defined classes.
When to Use std::move_iterator
You should use std::move_iterator when:
- Transferring ownership of objects between containers.
- Working with large objects where copying is expensive.
- Optimizing memory usage and performance.
- Using STL algorithms that normally perform copying operations.
Difference Between Normal Iterator and std::move_iterator
| Feature | Normal Iterator | std::move_iterator |
|---|---|---|
| Dereference returns | lvalue reference | rvalue reference |
| Operation | Copies elements | Moves elements |
| Source object | Remains unchanged | Becomes moved-from |
| Performance | Potentially expensive | Usually faster |