小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

vector賦值方法匯總

 fisher60 2012-08-06

c++的vector賦值方法匯總

  1. #include <iostream>   
  2.   
  3. #include <vector>   
  4.   
  5.   
  6. using namespace std;  
  7.   
  8. void main()  
  9.   
  10. {  
  11.   
  12.    vector<int>ivec1(10,42); //內(nèi)置方法,初始化的內(nèi)容為10個(gè)42   
  13.   
  14.    vector<int>ivec2(10);  
  15.   
  16.    vector<int>::size_type ix=0;  
  17.   
  18.    for(ix;ix<10;++ix)  
  19.   
  20.    {  
  21.   
  22.       ivec2[ix]=42; //下標(biāo)操作   
  23.   
  24.    }  
  25.   
  26.     vector<int>ivec3(10);  
  27.   
  28.    for(vector<int>::iterator iter=ivec3.begin();iter!=ivec3.end();++iter)  
  29.   
  30.    {  
  31.   
  32.       *iter=42; //迭代器   
  33.   
  34.    }  
  35.   
  36.   
  37.      /////下面兩種方法最佳,他們使用標(biāo)準(zhǔn)庫定義的操作,無須再定義vector對(duì)象時(shí)指定容器的大小。比較靈活且不容易出錯(cuò).   
  38.   
  39.    vector<int>ivec4;  
  40.   
  41.    vector<int>::iterator iter=ivec4.end();  
  42.   
  43.    for(int i=0;i!=10;++i)  
  44.   
  45.    {  
  46.   
  47.       ivec4.insert(iter,42); //在指定位置iter前插入值為的元素,返回指向這個(gè)元素的迭代器,    
  48.   
  49.       iter=ivec4.end();  
  50.   
  51.    }  
  52.   
  53.   
  54.   
  55.    vector<int>ivec5;   
  56.   
  57.    vector<int>::size_type cnt=1;  
  58.   
  59.    for(cnt;cnt<=10;++cnt)  
  60.   
  61.    {  
  62.   
  63.       ivec5.push_back(42); //push_back()添加值為的元素到當(dāng)前vector末尾   
  64.   
  65.    }  
  66.   
  67. }  
  68.    

 

 

下面還有一種賦值方法:通過數(shù)組指針給vector對(duì)象賦值:

如下:

  1. int myarray[5] = {1,3,5,7,9};  
  2. vector<int> myvector(myarray , myarray+5);  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多