I am trying to understand the move iterator. This is the sample code from https://en.cppreference.com/w/cpp/iterator/make_move_iterator
#include <iostream>
#include <list>
#include <vector>
#include <string>
#include <iterator>
int main()
{
std::vector<std::string> s{"one", "two", "three"};
std::vector<std::string> v1(s.begin(), s.end()); // copy
std::vector<std::string> v2(std::make_move_iterator(s.begin()),
std::make_move_iterator(s.end())); // move
std::cout << "v1 now holds: ";
for (auto str : v1)
std::cout << "\"" << str << "\" "; //return one, two, three
std::cout << "\nv2 now holds: ";
for (auto str : v2)
std::cout << "\"" << str << "\" "; //return one, two, three
std::cout << "\noriginal list now holds: ";
for (auto str : s)
std::cout << "\"" << str << "\" "; //return nothing
std::cout << '\n';
}
So the move iterator moves as expected. But when I made some minor changes:
std::vector<int> s{1, 2, 3};
std::vector<int> v1(s.begin(), s.end()); // copy
std::vector<int> v2(std::make_move_iterator(s.begin()),
std::make_move_iterator(s.end())); // move
s still references {1, 2, 3} despite moving. Is there a reason for this?