1. 程式人生 > >C++ vector 和push_back 詳解

C++ vector 和push_back 詳解

C++程式語言中有一種叫做Vector的應用方法。

(1)vector< 型別 > 識別符號 ;

(2)vector< 型別 > 識別符號(最大容量)

(3)vector< 型別 > 識別符號(最大容量,初始所有值);

(4) int i[4] = {12,3,4,5};

vector< 型別 > vi(i , i+2); //得到i索引值為3以後的值 ;  

(5)vector< vector<int> > //vi定義2維的容器;記得一定要有空格,不然會報錯

vector< int > line   // 在使用的時候一定要首先將vi個行進行初始化;

  for(int i = 0 ; i < 10 ; i ++)

 { 

vector.push_back(line); 

 }  /// 個人認為使用vector定義二維陣列很好,

因為是長度可以不預先確定。很好。 

(6)C++ Vector排序

vector< int > vi ;   

vi.push_back(1); 

vi.push_back(3);  

vi.push_back(0);

sort(vi.begin() , vi.end()); /// /小到大

 reverse(vi.begin(),vi.end()) /// 從大道小 

(7)順序訪問

vector < int

 > vi ;  

 for( int i = 0 ; i < 10 ; i ++) 

{  vector.push_back(i);  }  

 for(int i = 0 ; i < 10 ; i ++) /// 第一種呼叫方法 

 { cout <<vector[i] <<" " ;  } 

 for(vector<int>::iterator it = vi.begin() ; it !=vi.end() ; it++) ///第二種呼叫方法

  { cout << *it << " " ; } 

(8)尋找

vector <

 int > vi ; 

  for( int i = 0 ; i < 10 ; i ++)

 {  vector.push_back(i);  }   

vector < int >::interator it = find(vi.begin() , vi.end,3) ;

 cout << *it << endl ; ///返回容器內找到值的位置。 

(9)使用陣列對C++ Vector進行初始化

int i[10] ={1,2,3,4,5,6,7,78,8} ; ///第一種  

 vector<int> vi(i+1,i+3); ///從第2個元素到第三個元素

  for(vector <int>::interator it = vi.begin() ; it != vi.end() ; it++)

 {  cout << *it <<" " ;  } 

(10) 結構體型別

struct temp 

 { 

 public : string str ;  

 public :  int id ; 

};tmp  

int main()

  { 

 vector <temp> t ; 

 temp w1 ;  

 w1.str = "Hellowor" ;

 w1.id = 1 ;  

t.push_back(t1); 

cout << w1.str << "," <<w1.id <<endl ; 

 return 0 ;  

 } 

vector::push_back


public member function

void push_back ( const T& x );

Add element at the end

Adds a new element at the end of the vector,after its current last element. The content of this new element is initializedto a copy of x.Thiseffectively increases the vector sizeby one, which causes a reallocation of the internalallocated storage if the vector sizewas equal to the vector capacitybefore the call. Reallocations invalidate allpreviously obtained iterators, references and pointers.

Parameters

x

Value to be copied to the new element.
T is the first template parameter (the type of theelements stored in the vector).

Return value

noneIf areallocation happens, it is performed using Allocator::allocate(), which may throw exceptions (for thedefault allocatorbad_allocis thrown if the allocation request does notsucceed).

Example

//vector::push_back

#include<iostream>

#include<vector>

intmain ()

{

std::vector<int>myvector;

int myint;

std::cout << "Pleaseenter some integers (enter 0 to end):\n";

do {

std::cin >> myint;

myvector.push_back (myint);

} while (myint);

std::cout << "myvector stores" <<int(myvector.size()) << "numbers.\n";

return 0;

}

The example uses push_backto add a new element to the vector each time a new integeris read.

Complexity

Constant (amortized time, reallocation mayhappen).

See also

Delete last element (public member function)

Insert elements (public member function)