vector 基本用法

2021-09-11 22:11:18 字數 3719 閱讀 9306

/**********

* vector《型別》識別符號

vector《型別》識別符號(最大容量)

vector《型別》識別符號(最大容量,初始所有值)

int i[5]=

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

vector< vector< int> >v; 二維向量//這裡最外的<>要有空格。否則在比較舊的編譯器下無法通過

clear();//清除容器中所以資料 不會 釋放記憶體

data()//列印 記憶體區塊

push_back(i); //尾部 install data

pop.back(i) //delete top data

capacity()// 返回vector的實際儲存空間的大小,這個一般大於或等於vector元素個數,注意與size()函式的區別

#include //使用 swap

erase()// 直接用 會產生 野指標問題 配合 remove_if() 使用

vectora; //建立乙個空的的容器

vectorb(10,100); //建立乙個10個元素,每個元素值為100

vectorc(b.begin(),b.end()); //使用迭代器,可以取部分元素建立乙個新的容器

vectord(c); //複製建構函式,建立乙個完全一樣的容器

//過載=

vectore;

e = b; *

*/#include #include #include using namespace std;

class car

;bool cmp(int a,int b)

bool cmpp(car& c)

int main()

cout<< " clear vc_yq memory dir = "<< vc_yq.data()《執行結果:

0,1,2,3,4,push_back end

0,1,2,3,4,pop_back

iterator vec = 0

iterator vec = 1

iterator vec = 2

iterator vec = 3

iterator vec = 4

at fu = 0

at fu = 1

at fu = 2

at fu = 3

at fu = 4

=== 成員 交換===

reverse iterator vec = 4

reverse iterator vec = 3

reverse iterator vec = 2

reverse iterator vec = 1

reverse iterator vec = 0

===ascend sort 從小到大===

sort iterator vec = 0

sort iterator vec = 1

sort iterator vec = 2

sort iterator vec = 3

sort iterator vec = 4

===descend sort 從大到小===

descend iterator vec = 4

descend iterator vec = 3

descend iterator vec = 2

descend iterator vec = 1

descend iterator vec = 0

=== vector交換===

befor vec memory dir = 0x5585dfadd2e0

befor vc_yq memory dir = 0x5585dfadce70

after vec memory dir = 0x5585dfadce70

after vc_yq memory dir = 0x5585dfadd2e0

swap iterator vec = 6

swap iterator vec = 6

swap iterator vec = 6

swap iterator vec = 6

swap iterator vec = 6

swap iterator vc_yq = 4

swap iterator vc_yq = 3

swap iterator vc_yq = 2

swap iterator vc_yq = 1

swap iterator vc_yq = 0

=== unique 使用 ===

sort end = 1

sort end = 3

sort end = 4

sort end = 4

sort end = 9

sort end = 100

sort end = 100

sort end = 200

sort end = 300

sort end = 470

sort end = 600

*i = 1  *(i+1) = 3  unq.size() = 11 t =1 count =0

*i = 3  *(i+1) = 4  unq.size() = 11 t =2 count =0

*i = 4  *(i+1) = 4  unq.size() = 11 t =3 count =0

i  =4

*i = 9  *(i+1) = 100  unq.size() = 10 t =4 count =1

*i = 100  *(i+1) = 100  unq.size() = 10 t =5 count =1

i  =100

*i = 200  *(i+1) = 300  unq.size() = 9 t =6 count =2

*i = 300  *(i+1) = 470  unq.size() = 9 t =7 count =2

*i = 470  *(i+1) = 600  unq.size() = 9 t =8 count =2

*i = 600  *(i+1) = 600  unq.size() = 9 t =9 count =2

*************** ***************

befor unique  iterator unq = 10

*************** ***************

after unique  iterator unq = 1

after unique  iterator unq = 3

after unique  iterator unq = 4

after unique  iterator unq = 9

after unique  iterator unq = 100

after unique  iterator unq = 200

after unique  iterator unq = 300

after unique  iterator unq = 470

after unique  iterator unq = 600

size  vec memory dir = 0

capacity  vec memory dir = 5

=== vector release memory===

clear  vc_yq memory dir = 0

clear  vec memory dir = 0

vector類基本用法

vector類為內建陣列提供了一種替代表示,與string類一樣 vector 類是隨標準 c 引入的標準庫的一部分 為了使用vector 我們必須包含相關的標頭檔案 include 使用vector有兩種不同的形式,即所謂的陣列習慣和 stl習慣。1.定義乙個已知長度的 vector vector...

vector的基本用法

vector是c 標準模板庫中的部分內容,它是乙個多功能的,能夠操作多種資料結構和演算法的模板類和函式庫。vector之所以被認為是乙個容器,是因為它能夠像容器一樣存放各種型別的物件,簡單地說,vector是乙個能夠存放任意型別的動態陣列,能夠增加和壓縮資料。標頭檔案 include 宣告 乙個in...

vector的基本用法

include include vector的標頭檔案 using namespace std int main if num.empty 判斷容器是否為空,為不的意思 num.pop back 去掉尾部的最後乙個元素 num.at 2 88 索引的方式修改第二個元素為88 num 5 66 下標的...