2 vector是將元素置于一個(gè)動態(tài)數(shù)組中加以管理的容器。
2 vector可以隨機(jī)存取元素(支持索引值直接存取, 用[]操作符或at()方法,這個(gè)等下會詳講)。
vector尾部添加或移除元素非常快速。但是在中部或頭部插入元素或移除元素比較費(fèi)時(shí)
vector采用模板類實(shí)現(xiàn),vector對象的默認(rèn)構(gòu)造形式
vector<T> vecT;
vector<int> vecInt; //一個(gè)存放int的vector容器。
vector<float> vecFloat; //一個(gè)存放float的vector容器。
vector<string> vecString; //一個(gè)存放string的vector容器。
... //尖括號內(nèi)還可以設(shè)置指針類型或自定義類型。
Class CA{};
vector<CA*> vecpCA; //用于存放CA對象的指針的vector容器。
vector<CA> vecCA; //用于存放CA對象的vector容器。由于容器元素的存放是按值復(fù)制的方式進(jìn)行的,所以此時(shí)CA必須提供CA的拷貝構(gòu)造函數(shù),以保證CA對象間拷貝正常。
理論知識
2 vector(beg,end); //構(gòu)造函數(shù)將[beg, end)區(qū)間中的元素拷貝給本身。注意該區(qū)間是左閉右開的區(qū)間。
2 vector(n,elem); //構(gòu)造函數(shù)將n個(gè)elem拷貝給本身。
2 vector(const vector &vec); //拷貝構(gòu)造函數(shù)
int iArray[] = {0,1,2,3,4};
vector<int> vecIntA( iArray, iArray+5 );
vector<int> vecIntB ( vecIntA.begin() , vecIntA.end() ); //用構(gòu)造函數(shù)初始化容器vecIntB
vector<int> vecIntB ( vecIntA.begin() , vecIntA.begin()+3 );
vector<int> vecIntC(3,9); //此代碼運(yùn)行后,容器vecIntB就存放3個(gè)元素,每個(gè)元素的值是9。
vector<int> vecIntD(vecIntA);
理論知識
2 vector.assign(beg,end); //將[beg, end)區(qū)間中的數(shù)據(jù)拷貝賦值給本身。注意該區(qū)間是左閉右開的區(qū)間。
2 vector.assign(n,elem); //將n個(gè)elem拷貝賦值給本身。
2 vector& Operator=(const vector &vec); //重載等號操作符
2 vector.swap(vec); // 將vec與本身的元素互換。
vector<int> vecIntA, vecIntB, vecIntC, vecIntD;
int iArray[] = {0,1,2,3,4};
vecIntA.assign(iArray,iArray+5);
vecIntB.assign( vecIntA.begin(), vecIntA.end() ); //用其它容器的迭代器作參數(shù)。
vecIntC.assign(3,9);
vector<int> vecIntD;
vecIntD = vecIntA;
vecIntA.swap(vecIntD);
理論知識
2 vector.size(); //返回容器中元素的個(gè)數(shù)
2 vector.empty(); //判斷容器是否為空
2 vector.resize(num); //重新指定容器的長度為num,若容器變長,則以默認(rèn)值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除。
2 vector.resize(num, elem); //重新指定容器的長度為num,若容器變長,則以elem值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除。
例如 vecInt是vector<int> 聲明的容器,現(xiàn)已包含1,2,3元素。
int iSize = vecInt.size(); //iSize == 3;
bool bEmpty = vecInt.empty(); // bEmpty == false;
執(zhí)行vecInt.resize(5); //此時(shí)里面包含1,2,3,0,0元素。
再執(zhí)行vecInt.resize(8,3); //此時(shí)里面包含1,2,3,0,0,3,3,3元素。
再執(zhí)行vecInt.resize(2); //此時(shí)里面包含1,2元素。
vector<int> vecInt;
vecInt.push_back(1); //在容器尾部加入一個(gè)元素
vecInt.push_back(3); //移除容器中最后一個(gè)元素
vecInt.push_back(5);
vecInt.push_back(7);
vecInt.push_back(9);
vecInt.pop_back();
vecInt.pop_back();
//{5 ,7 ,9}
理論知識
vec.at(idx); //返回索引idx所指的數(shù)據(jù),如果idx越界,拋出out_of_range異常。
vec[idx]; //返回索引idx所指的數(shù)據(jù),越界時(shí),運(yùn)行直接報(bào)錯(cuò)
vector<int> vecInt; //假設(shè)包含1 ,3 ,5 ,7 ,9
vecInt.at(2) == vecInt[2] ; //5
vecInt.at(2) = 8; 或 vecInt[2] = 8;
vecInt 就包含 1, 3, 8, 7, 9值
int iF = vector.front(); //iF==1
int iB = vector.back(); //iB==9
vector.front() = 11; //vecInt包含{11,3,8,7,9}
vector.back() = 19; //vecInt包含{11,3,8,7,19}
測試代碼
#include <iostream>using namespace std;#include "vector"void main1(){ vector<int> v1;//構(gòu)造一個(gè)容器v1 cout << "v1 before length:" << endl; cout << v1.size() << endl;//得到容器v1的長度 v1.push_back(1);//將數(shù)據(jù)加入到容器中 v1.push_back(2); v1.push_back(3); cout << "v1 after length:" << endl; cout << v1.size() << endl;//測量加入數(shù)據(jù)后的容器大小 cout << "頭部元素" << v1.front() << endl; v1.front() = 11;//修改頭部元素 v1.back() = 55;//修改尾部元素 while (v1.size() > 1) { cout << "尾部元素為:" << v1.back() << endl; v1.pop_back();//刪除尾部元素 } cout << v1.size() << endl;//得到容器v1的長度}int main(){ main1(); return 0;}測試結(jié)果:
新聞熱點(diǎn)
疑難解答
圖片精選