I have questions regarding make_move_iterator
. First when make_move_iterator
is used in f2()
, similar to Stroustrup C++ 4th Ed Page 964, it will not compile. Does anyone know if this is it's correct use and why it wont compile?
Also, in the other examples, I expected data to be moved out of the source vector, however the results show that it remains. Is this expected?
#include <iterator>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void f0()
{
// Use constructor method
vector<int> v {0, 1, 2, 3};
vector<int> v2 {make_move_iterator(v.begin()),
make_move_iterator(v.end())};
cout << endl << "f0: v orig (should be moved from)" << endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, ","));
cout << endl << "f0: v2 copy output" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ","));
cout << endl;
}
void f1()
{
vector<int> v {0, 1, 2, 3};
vector<int> v2;
// Use copy method
copy(make_move_iterator(v.begin()), make_move_iterator(v.end()),
back_inserter(v2));
cout << "f1: orig output (should be moved from)" << endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, ","));
cout << endl << "f1: copy output" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ","));
cout << endl << "f1: end copy output" << endl;
}
void f2()
{
vector<int> v {0, 1, 2, 3};
vector<int> v2;
// will not compile, example in Stroustrup C++ 4th Ed Page 964
// copy(v.begin(), v.end(), make_move_iterator(back_inserter(v2)));
}
void f3()
{
vector<int> v {0, 1, 2, 3};
vector<int> v2(v.size());
// Use copy method
copy(make_move_iterator(v.begin()), make_move_iterator(v.end()),
v2.begin());
cout << "f1: orig output (should be moved from)" << endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, ","));
cout << endl << "f1: copy output" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ","));
cout << endl << "f1: end copy output" << endl;
}
int main()
{
f0(); f1(); f2(); f3();
return 0;
}
Compilation and results:
clang++ -std=c++11 -pedantic -Wall test235.cc && ./a.out
f0: v orig (should be moved from)
0,1,2,3,
f0: v2 copy output
0,1,2,3,
f1: orig output (should be moved from)
0,1,2,3,
f1: copy output
0,1,2,3,
f1: end copy output
f3: orig output (should be moved from)
0,1,2,3,
f3: copy output
0,1,2,3,
f3: end copy output
Compilation finished at Sun Aug 2 21:47:27