In practice, the vector series in c ++ -- vector (unique_ptr () is assigned to vector (unique_ptr ())
Previously, we mentioned that the insert method can be used to copy a vector to the end of another vector.
As mentioned in previous blogs, if unique_ptr is placed inside the vector container, ownership transfer is required.
Now, how are you doing?vector > insert to vector >
If the regular vector is used, we can use insert as follows:
// Inserting into a vector # include
# Include
Int main () {std: vector
Myvector (3,100); // 100 100 100 std: vector
: Iterator it; it = myvector. begin (); it = myvector. insert (it, 200); // 200 100 100 100 myvector. insert (it, 2,300); // 300 300 200 100 100 100 // "it" no longer valid, get a new one: it = myvector. begin (); std: vector
Anothervectors (2,400); myvector. insert (it + 2, anothervector. begin (), anothervector. end (); // now, 300 300 400 400 200 100 100 100 int myarray [] = {501,502,503}; myvector. insert (myvector. begin (), myarray, myarray + 3); std: cout <"myvector contains:"; for (it = myvector. begin (); it <myvector. end (); it ++) = "" std: cout = "" <= "" '= "" * it; = ""' \ n '; = "" return = "" 0; = ""} = "" output: = "" 501 = "" 502 = "" 503 = "" 300 = "" 400 = "" 200 = "" 100 = ""
However, for the unique point in the vector, you cannot simply use the normal iterator: You need to use the iterator to perform the std: make_move_iterator operation:
The English description is the most reliable:
A move_iterator is an iterator adaptor that adapts an iterator (it) so that dereferencing it produces rvalue references (as if std: move was applied), while all other operations behave the same.
It works the same way as std: move we used before ~
Let's take a look at the example:
# 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;} // output: foo: one two three
Next, let's use it. It's easy:
# 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; return 0;} void display_vector (vector > & Vec) {for (auto it = vec. begin (); it! = Vec. end (); it ++) {cout <** it <endl;} // output result: 51234now, des_vec size: 5now, vec size: 4