codepromo0.fr
MasterofProject

Actual combat vector in the c++ series -- talk about the insert vector () method (all make_move_iterator blame)

Label VectorC++
691 people read comments(0) Collection Report
Classification:

Prior to the insert on the vector () method, the B vector elements into the A vector, A vector in the results we can imagine, but B vector in the elements will be how?

Take a look at the previously written procedures:

<iostream> #include
<vector> #include

IntMain ()
{
  STD::Vector<Int>MyVector (Three,One hundred);
  STD::Vector<Int>: it iterator;

It = myvector.begin ();
It = myvector.insert (it,Two hundred);

Myvector.insert (it,Two,Three hundred);

  "It" no longer / valid get a new one:
It = myvector.begin ();

  STD::Vector<Int>Anothervector (Two,Four hundred);
Myvector.insert (it+Two(anothervector.begin), anothervector.end (), ();

  IntMyArray [] = {Five hundred and one,Five hundred and two,Five hundred and three};
Myvector.insert (myvector.begin) (), myArray, myarray+Three);

  STD::Cout""Contains: myvector";
  For(it=myvector.begin (); it<myvector.end (); it++ ()
    STD::Cout"''*it;
  STD::Cout"'\n';

  Return Zero;
}

Now care about something else: note that the insert is, the number of vector by insert:

<iostream> #include
<vector> #include

Int(main)
{
    STD::Vector<Int>MyVector (Three,One hundred);

    STD::Vector<Int>Anothervector (Two,Four hundred);

    STD::Cout""Insert MyVector is: Before";
    For(AutoMyvector.begin = it (); it<myvector.end (); it++)
        STD::Cout"''*it;
    STD::Cout"STD: endl;

    STD::Cout""Insert anothervector is: Before";
    For(AutoAnothervector.begin = it (); it<anothervector.end (); it++)
        STD::Cout"''*it;

    STD::Cout"STD: endl;

Myvector.insert (myvector.end (), anothervector.begin (), anothervector.end (), ();

    STD::Cout""Insert MyVector is: After";
    For(AutoMyvector.begin = it (); it<myvector.end (); it++)
        STD::Cout"''*it;

    STD::Cout"STD: endl;

    STD::Cout""The anothervector is: Now";
    For(AutoAnothervector.begin = it (); it<anothervector.end (); it++)
        STD::Cout"''*it;

    STD::Cout"STD: endl;

    IntMyarray[] = {Five hundred and one,Five hundred and two,Five hundred and three};
Myvector.insert (myvector.begin) (), myArray, myArray +Three);

    STD::Cout""Insert myarray[] to MyVector MyVector, is: After";
    For(AutoMyvector.begin = it (); it<myvector.end (); it++)
        STD::Cout"''*it;

    STD::Cout"STD: endl;

    STD::Cout""Insert myarray[] to MyVector myarray[], is: After";
    For(Int= IZeroI; "ThreeI++);
{
        STD::Cout"''Myarray[i];
}

    Return Zero;
}
/ / output:
Insert MyVector is //Before: 100100100
Insert anothervector is //Before: 400400
Insert MyVector is //After: 100100100400400
The anothervector is //Now: 400400
Insert myarray[] to MyVector //After, is MyVector: 400400 501502503100100100
Insert myarray[] to MyVector //After, is myarray[]: 501502503

If you see this, you will in the heart maniang who cares about vector, B, and vectorB did not change.
Now is the time to put the point of illness, vector smart pointer.

Before the blog has also told the vector elements for the smart pointer:

#include<iostream>
#include<vector>
<memory> #include
Using Namespace STD;
VoidDisplay_vector (Vector<unique_ptr<Int>> &vec);
Int(main)
{
    Vector<unique_ptr<Int>> vec;
Unique_ptr<Int(> S1New Int(One));
Unique_ptr<Int(> S2New Int(Two));
Unique_ptr<Int(> S3New Int(Three));
Unique_ptr<Int(> S4New Int(Four));
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(> S5New Int(Five));
    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 ());
矢量显示_(DES _ VEC);
    cout<<“现在,DES _ VEC的大小:《_(DES)vec.size << endl;
    cout<<“现在,VEC的大小:<< << endl;vec.size()

    //显示_向量(矢量);
    cout<<“现在,VEC的大小:<< << endl;vec.size()
    国际i =0vec.size;i<();i++)
{
        cout*〔i〕<<<<作用“”的;
}
    返回 0的;
}

无效_向量显示矢量独特的_)<<国际>>(VEC)
{
    它vec.begin =();!它vec.end)=(+);
{
        cout××××××××××××××是<< << endl;
}
}

上面代码会崩溃,原因就是vec被insert后,vec变得无效了,我们不能对他做什么…..

但是需要明确的是这不是insert造成的,如果copy也会造成这一的结局,其实罪魁祸首就是让_ _移动迭代器

再看程序:

摘要:# >
# include<列表
矢量# include<>
#包括字符串> <
#>有<迭代器

国际main()
{
    性病:::::列表性病:::::字符串>S {“一个”“两个”“三”};

    性病:::::矢量性病:::::字符串>(s.begin s.end V1的(),());//拷贝

    性病:::::矢量性病:::::字符串>V2(性病组成:移动_ _迭代器(s.begin()))))
        性病组成:移动_ _迭代器(s.end())))));//移动

    性病:::::cout<<“现在是V1的:”的;
    1)乙方:
        性病:::::cout<<“\”“<<<<STR“\”“的;
    性病:::::cout<<“现在:NV2持有”的;
    2)乙方:
        性病:::::cout<<“\”“<<<<STR“\”“的;
    性病:::::cout<<“现在是noriginal列表\”的;
    乙方:(S)
        性病:::::cout<<“\”“<<<<STR“\”“的;
    性病:::::cout<<'\n'的;
}
//exportation:
//V1的:现在是“一”“二”“三”
//V2现在认为:“一”“二”“三”
将原始列表/:“现在”

最后再上一个官方程序:

摘要:# >//性病::.
#>有<迭代器性病::让_ /移动_迭代器
矢量# include<>//性病::向量
#包括字符串> <//标准::字符串
#>有<算法拷贝std::/ /

国际main(){
    性病:::::矢量性病:::::字符串>foo(3);
    性病:::::矢量性病:::::字符串>{酒吧“一个”“两个”“三”};

    性病组成:复制(移动_ _迭代器(bar.begin()))))
让_ _迭代器(bar.end(移动)
foo.begin());

    现在unspecified /酒吧清除它的值最高;
bar.clear();

    性病:::::cout<<“foo”的;
    性病:::::字符串与X:Foo)性病:::::cout<<'''<<x;
    性病:::::cout<<'\n'的;

    返回 0的;
}

需要注意:××××××××××××××
××××××××××××××bar.clear();
因此此时:酒吧现在包含unspecified清除它的值;

1
0
猜你在找
查看评论
×××××××以上用户言论只代表其个人观点,不代表csdn网站的观点或立场
    个人资料
    • 访问:135283次
    • 积分:3820
    • 等级:
    • 排名:第4651名
    • 原创:218篇
    • 转载:15篇
    • 原文:6篇
    • 评论:159条
    博客专栏
    文章分类
    文章存档
    最新评论
gutscheincode0.de