Vector series--vector<unique_ptr<>> assignment to vector<unique_ptr<>> in combat C + +

Source: Internet
Author: User

Previous blogs about vectors you can use the Insert method to copy a vector to the back of another vector.

Previous blogs have also talked about the need to transfer ownership if the vector container is placed inside the unique_ptr.

Come on, now. 818 Howvector<unique_ptr<>> insert to vector<unique_ptr<>>

If you have a regular vector, we can use insert like this:

//inserting into a vector#include <iostream>#include <vector>intMain () {STD:: vector<int>Myvector (3, -);//100  STD:: vector<int>:: Iterator it;  it = Myvector.begin (); it = Myvector.insert (it, $);//200Myvector.insert (IT,2, -);//300  //"It" no longer valid, get a new one:it = Myvector.begin ();STD:: vector<int>Anothervector (2, -); Myvector.insert (it+2, Anothervector.begin (), Anothervector.end ());//now, A.  intMyArray [] = {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;STD::cout<<' \ n ';return 0;}//output:501 502 503  -  -  -  -  $  -  -  -

But for a unique point within a vector, you can't simply use a normal iterator: you need to use the Std::make_move_iterator action on the iterator:

Look at the most reliable English description:
A move_iterator is an iterator adaptor that adapts an iterator (it) so the dereferencing it produces rvalue references (a s if Std::move is applied), while all other operations behave the same.

Just like the std::move we used before.
See examples of Use:

#include <iostream> //std::cout #include <iterator> //Std::make_move_iterator #include <vector> //std::vector #include <string> //std::string #include <algorithm> //std::copy intMain () {STD:: Vector<std::string>Foo (3);STD:: Vector<std::string>Bar {"One","both","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 Three

The next step is our use, which is simple:

#include <iostream>#include <vector>#include <memory>using namespace STD;voidDisplay_vector ( vector<unique_ptr<int>> &vec);intMain () { vector<unique_ptr<int>> VEC; unique_ptr<int> S1 (New int(1)); unique_ptr<int> s2 (New int(2)); unique_ptr<int> S3 (New int(3)); unique_ptr<int> 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<int> S5 (New int(5)); vector<unique_ptr<int>> 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;}voidDisplay_vector ( vector<unique_ptr<int>> &vec) { for(Autoit = Vec.begin (); It! = Vec.end (); it++) {cout<< **it << Endl; }//Output Result:51234Now, Des_vec size:5Now, VEC size:4

Vector series--vector<unique_ptr<>> assignment to vector<unique_ptr<>> in combat C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.