天天看點

标準模闆庫(STL)學習指南之vector向量

vector

– 一.  vector可以模拟動态數組

– 二.  vector的元素可以是任意類型T,但必須具備指派和拷貝能力(具有public

       拷貝構造函數和重載的指派操作符)

    三.必須包含的頭檔案#include <vector>

–        四.  vector支援随機存取

–        五.  vector的大小(size)和容量(capacity)通常是不同的,size傳回實際元素個數,

               capacity傳回vector能容納的元素最大數量。如果插入元素時,元素個數超過capacity,

       需要重新配置内部存儲器。 

标準模闆庫(STL)學習指南之vector向量
->構造、拷貝和析構 
标準模闆庫(STL)學習指南之vector向量
 ->非變動操作
标準模闆庫(STL)學習指南之vector向量
 eg.
标準模闆庫(STL)學習指南之vector向量

vector<int> v1(10);
           cout                << "The capacity of v1 is " << v1.capacity() << endl;
           cout                << "The size of v1 is " << v1.size() << endl;
   vector<int> v2;
   v2.reserve(20);
           cout                << "The capacity of v2 is " << v2.capacity() << endl;
           cout                << "The size of v2 is " << v2.size() << endl;      
output :      
The capacity of v1 is 10
   The size of v1 is 10
   The capacity of v2 is 20
   The size of v2 is 0      
标準模闆庫(STL)學習指南之vector向量

->指派操作

标準模闆庫(STL)學習指南之vector向量

所有的指派操作都有可能調用元素類型的預設構造函數,拷貝構造函數,指派操作符和析構函數

如: 

  std::list<T> l;

  std::vector<T> v;

  …

  v.assign(l.begin(),l.end());

eg.    

标準模闆庫(STL)學習指南之vector向量
vector<int> v;
   v.assign( 10, 42 );
   for( vector<int>::size_type i = 0; i < v.size(); i++ ) {
             cout                << v[i] << " ";
   }
           cout                << endl;      

OutPut : 

42 42 42 42 42 42 42 42 42 42

vector<int> v1;
   for( int i = 0; i < 10; i++ ) {
     v1.push_back( i );
   }
 
   vector<int> v2;
   v2.assign( v1.begin(), v1.end() );
 
   for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
             cout                << v2[i] << " ";
   } 
      
cout                << endl;       

output :

0 1 2 3 4 5 6 7 8 9  

标準模闆庫(STL)學習指南之vector向量

元素存取 

标準模闆庫(STL)學習指南之vector向量

 下面的操作是錯誤的:

      std::vector<T> v;//empty

  v[5]= t; //runtime error

  std::cout << v.front(); //runtime error

eg. 

标準模闆庫(STL)學習指南之vector向量

  vector<string> words;

string str;
 
    while( cin >> str ) words.push_back(str);
 
    sort( words.begin(), words.end() );
 
            cout                << "In alphabetical order, the first word is '" << words.front() << "'." << endl;      
假設輸入是:  now is the time for all good men to come to the aid of their country      
output:      
In alphabetical order, the first word is 'aid'.      
vector<int> v;
   for( int i = 0; i < 5; i++ ) {
     v.push_back(i);
   }
           cout                << "The first element is " << v.front()
        << " and the last element is " << v.back() << endl;      
output:      
The first element is 0 and the last element is 4      
标準模闆庫(STL)學習指南之vector向量

 疊代器相關函數

标準模闆庫(STL)學習指南之vector向量

使用疊代器時應注意:

疊代器持續有效,除非發生以下兩種情況:

1.) 或插入元素

2.) 容量變化而引起記憶體重新配置設定

标準模闆庫(STL)學習指南之vector向量
vector<string> words;
    string str;
 
    while( cin >> str ) words.push_back(str);
 
    for( vector<string>::const_iterator iter = words.begin();
         iter != words.end(); ++iter ) {
              cout                << *iter << endl;
    }      

 假設輸入是 :  hey mickey you're so fine 

 output:

    hey        
mickey
    you're
    so
    fine      
标準模闆庫(STL)學習指南之vector向量

 插入(insert)元素

标準模闆庫(STL)學習指南之vector向量
标準模闆庫(STL)學習指南之vector向量
vector<char> alphaVector;
   for( int i=0; i < 10; i++ ) {
     alphaVector.push_back( i + 'A' );
   }
 
   // Insert four C's into the vector
   vector<char>::iterator theIterator = alphaVector.begin();
   alphaVector.insert( theIterator, 4, 'C' );
 
   // Display the vector
   for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); ++theIterator )    {
             cout                << *theIterator;
   }      

CCCCABCDEFGHIJ 

vector<int> v1;
    v1.push_back( 0 );
    v1.push_back( 1 );
    v1.push_back( 2 );
    v1.push_back( 3 );
 
    vector<int> v2;
    v2.push_back( 5 );
    v2.push_back( 6 );
    v2.push_back( 7 );
    v2.push_back( 8 );
 
            cout                << "Before, v2 is: ";
    for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
              cout                << v2[i] << " ";
    }
            cout                << endl;
 
    v2.insert( v2.end(), v1.begin(), v1.end() );
 
            cout                << "After, v2 is: ";
    for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
              cout                << v2[i] << " ";
    } 
      
cout                << endl;       
Before, v2 is: 5 6 7 8       
After, v2 is: 5 6 7 8 0 1 2 3       
标準模闆庫(STL)學習指南之vector向量

–删除(remove)元素

标準模闆庫(STL)學習指南之vector向量
标準模闆庫(STL)學習指南之vector向量
vector<char> alphas;
    for( int i=0; i < 10; i++ ) {
      static const char letters[] = "ABCDEFGHIJ";
      alphas.push_back( letters[i] );
    }
    vector<char>::size_type size = alphas.size();
    vector<char>::iterator startIterator;
    vector<char>::iterator tempIterator;
    for( vector<char>::size_type i=0; i < size; i++ ) {
      startIterator = alphas.begin();
      alphas.erase( startIterator );
      // Display the vector
      for( tempIterator = alphas.begin(); tempIterator != alphas.end(); ++tempIterator ) {
                cout                << *tempIterator;
      }
              cout                << endl;
    }      
BCDEFGHIJ
    CDEFGHIJ
    DEFGHIJ
    EFGHIJ
    FGHIJ
    GHIJ
    HIJ
    IJ       
J      
vector<char> alphas;
    for( int i=0; i < 10; i++ ) {
      static const char letters[] = "ABCDEFGHIJ";
      alphas.push_back( letters[i] );
    }
    // display the complete vector
    for( vector<char>::size_type i = 0; i < alphas.size(); i++ ) {
              cout                << alphas[i];
    }
            cout                << endl;
 
    // use erase to remove all but the first two and last three elements
    // of the vector
    alphas.erase( alphas.begin()+2, alphas.end()-3 );
    // display the modified vector
    for( vector<char>::size_type i = 0; i < alphas.size(); i++ ) {
              cout                << alphas[i];
    } 
      
cout                << endl;      
ABCDEFGHIJ       
ABHIJ       
#include <iostream>
#include <vector>
#include <iterator>
 
using namespace std;
 
int main()
{
    vector<char> alphas;
    for( int i=0; i < 10; i++ ) {
      static const char letters[] = "ABCDEFGHIJ";
      alphas.push_back( letters[i] );
    }
 
    vector<char>::iterator iter = alphas.begin();
    while( iter != alphas.end() )
    {
      if (*iter == 'B' || *iter == 'D')
        iter = alphas.erase( iter );
      else
        ++iter;
    }
 
    copy(alphas.begin(), alphas.end(), ostream_iterator<char>(        cout               , ""));
            cout                << endl;
  }       

ACEFGHIJ 

标準模闆庫(STL)學習指南之vector向量

 轉載自:

http://www.cppblog.com/MiYu/archive/2010/08/31/125459.html

作者:

ACShiryu

出處:

http://www.cnblogs.com/ACShiryu/

若非注明,本部落格文章均為原創,版權歸作者和部落格園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連結,否則保留追究法律責任的權利。 

該文章也同步釋出在我的新浪微網誌中-

ACShiryu's weibo

,歡迎收聽。

繼續閱讀