The vector series in combat C + +-talk about the vector's insert () method (all Make_move_iterator)

Source: Internet
Author: User

Previously, the Insert () method for vectors was used to insert the elements of vector b into vector a. We can imagine the results in vector a, but what about the elements in vector b?

Look at the program that you've written before:

#include <iostream>#include <vector>intMain () {STD:: vector<int>Myvector (3, -);STD:: vector<int>:: Iterator it;  it = Myvector.begin (); it = Myvector.insert (it, $); Myvector.insert (IT,2, -);//"It" no longer valid, get a new one:it = Myvector.begin ();STD:: vector<int>Anothervector (2, -); Myvector.insert (it+2, Anothervector.begin (), Anothervector.end ());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;}

Now take care of something else: note how much of the vector is insert after insert:

#include <iostream>#include <vector>intMain () {STD:: vector<int>Myvector (3, -);STD:: vector<int>Anothervector (2, -);STD::cout<<"Before insert Myvector is:"; for(Autoit = Myvector.begin (); It<myvector.end (); it++)STD::cout<<"'<< *it;STD::cout<<STD:: Endl;STD::cout<<"Before insert Anothervector is:"; for(Autoit = Anothervector.begin (); It<anothervector.end (); it++)STD::cout<<"'<< *it;STD::cout<<STD:: Endl; Myvector.insert (Myvector.end (), Anothervector.begin (), Anothervector.end ());STD::cout<<"After insert Myvector is:"; for(Autoit = Myvector.begin (); It<myvector.end (); it++)STD::cout<<"'<< *it;STD::cout<<STD:: Endl;STD::cout<<"Now the Anothervector is:"; for(Autoit = Anothervector.begin (); It<anothervector.end (); it++)STD::cout<<"'<< *it;STD::cout<<STD:: Endl;intMyarray[] = {501,502,503}; Myvector.insert (Myvector.begin (), myarray, MyArray +3);STD::cout<<After insert myarray[-Myvector, Myvector is: "; for(Autoit = Myvector.begin (); It<myvector.end (); it++)STD::cout<<"'<< *it;STD::cout<<STD:: Endl;STD::cout<<After insert myarray[-Myvector, myarray[] is: "; for(inti =0; I <3; i++) {STD::cout<<"'<< Myarray[i]; }return 0;}//output://before Insert Myvector is:100//before Insert Anothervector is:400//after Insert Myvector is:100//now the Anothervector is:400//after Insert myarray[] to Myvector, Myvector is:501 502 503//after Insert myarray[] to Myvector, myarray[] is:501 502 503

Assuming you see this, you're sure to dozens in your heart, who cares about vector B, and VectOrb doesn't change.


Now it's time to have some curtailing. Put a smart pointer in the vector.

Previous blogs have also told you that the elements of vectors are smart pointers:

#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;//display_vector (VEC);    cout<<"Now, VEC size:"<< vec.size () << Endl; for(intI=0; I<vec.size (); i++) {cout<< *vec[i] <<" "; }return 0;}voidDisplay_vector ( vector<unique_ptr<int>> &vec) { for(Autoit = Vec.begin (); It! = Vec.end (); it++) {cout<< **it << Endl; }}

The above code will crash. The reason is that VEC was insert after. VEC became ineffective, and we couldn't do anything to him ....

But it is necessary to understand that this is not caused by insert, assuming that copy will also cause this outcome, in fact, the culprit is make_move_iterator

Then look at the program:

#include <iostream>#include <list>#include <vector>#include <string>#include <iterator>intMain () {STD:: List<std::string>s{"One","both","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(AutoSTR:V1)STD::cout<<"\""<< Str <<"\" ";STD::cout<<"\nv2 now holds:"; for(AutoSTR:V2)STD::cout<<"\""<< Str <<"\" ";STD::cout<<"\noriginal list now holds:"; for(AutoSTR:S)STD::cout<<"\""<< Str <<"\" ";STD::cout<<' \ n ';}//output://v1 now holds: "One" "both" "Three "//v2 now holds: "One" "both" "Three "//original list now holds: ""

Last official procedure:

#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>Foo3);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;}

* Need to NOTE: *
* Bar.clear (); *
So at this point: bar now contains unspecified values; Clear it:

The vector series in combat C + +-talk about the vector's insert () method (all Make_move_iterator)

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.