In practice, the vector series in c ++ -- let's talk about the insert () method of the vector (all of which are caused by make_move_iterator)
I have mentioned the insert () method of vector before and insert the element of vector B into vector A. we can imagine the results in vector A, but what about the elements in vector B?
Check out the previously written programs:
#include
#include
int main (){ std::vector
myvector (3,100); std::vector
::iterator it; it = myvector.begin(); it = myvector.insert ( it , 200 ); myvector.insert (it,2,300); // "it" no longer valid, get a new one: it = myvector.begin(); std::vector
anothervector (2,400); myvector.insert (it+2,anothervector.begin(),anothervector.end()); int myarray [] = { 501,502,503 }; myvector.insert (myvector.begin(), myarray, myarray+3); std::cout << "myvector contains:"; for (it=myvector.begin(); it
If you see that at this time, you will surely swear at it, who cares about vector B, and vectorB has not changed.
Now it's time to take a try. Put the smart pointer in the vector.
Previously, my blog mentioned that when the element of a vector is a smart pointer:
#include #include #include using namespace std;void display_vector(vector > &vec);int main(){ vector > vec; unique_ptr s1(new int(1)); unique_ptr s2(new int(2)); unique_ptr s3(new int(3)); unique_ptr s4(new int(4)); vec.push_back(std::move(s1)); vec.push_back(std::move(s2)); vec.push_back(std::move(s3)); vec.push_back(std::move(s4)); unique_ptr s5(new int(5)); vector > des_vec; des_vec.push_back(std::move(s5)); des_vec.insert(des_vec.end(), std::make_move_iterator(vec.begin()), std::make_move_iterator(vec.end())); display_vector(des_vec); cout << "now, des_vec size: " << des_vec.size() << endl; cout << "now, vec size: " << vec.size() << endl; //display_vector(vec); cout << "now, vec size: " << vec.size() << endl; for (int i=0; i > &vec){ for (auto it = vec.begin(); it != vec.end(); it++) { cout << **it << endl; }}
The code above will crash because after vec is inserted, vec becomes invalid and we cannot do anything to it .....
But it should be clear that this is not caused by insert. If copy will also cause this ending, the culprit isMake_move_iterator
Check the program again:
# Include # Include # Include # Include # Include Int main () {std: list S {"one", "two", "three"}; std: vector V1 (s. begin (), s. end (); // copy std: vector 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 <" \ ""; std: cout <"\ nv2 now holds:"; for (auto str: v2) std:: cout <"\" "<str <" \ ""; std: cout <"\ noriginal list now holds:"; for (auto str: s) std: cout <"\" "<str <" \ ""; std: cout <'\ n';} // output: // v1 now holds: "one" "two" "three" // v2 now holds: "one" "two" "three" // original list now holds :""
Finally, go to an official program:
#include // std::cout#include // std::make_move_iterator#include // std::vector#include // std::string#include // std::copyint main() { std::vector foo(3); std::vector bar{ "one","two","three" }; std::copy(make_move_iterator(bar.begin()), make_move_iterator(bar.end()), foo.begin()); // bar now contains unspecified values; clear it: bar.clear(); std::cout << "foo:"; for (std::string& x : foo) std::cout << ' ' << x; std::cout << '\n'; return 0;}
* Note :*
* Bar. clear ();*
So now: bar now contains unspecified values; clear it: