vector類型介紹


一、vector類型簡介

標准庫:集合或動態數組,我們可以放若干對象放在里面。

vector他能把其他對象裝進來,也被稱為容器

#include <iostream>
#include <vector>
using namespace std;

struct student{int num;};

int main()
{
    vector<int> vjihe; // 表示這個集合對象保存的是int型數據
    // <int>:模板,vector本身就是個類模板,<int>實際上就是類模板的實例化過程。
    vector<student> vstudlist;// 存放學生類型的集合
    vector<vector<string>> strchuan; // 相當於二維的字符串。
    vector<int *> vjihe2;
    // 不能向集合中裝引用
    //vector<int &> vjihe3; // 引用知識一個別名,不是對象。
    return 0;
}

二、定義和初始化vector對象

(1)空的vector

vector<string> mystr; // 創建一個string類型的空的集合
//push_back()
mystr.push_back(“abc”);
mystr.push_back(“efg”);

(2)元素拷貝的初始化方式

vector<string> mystr2(mystr); // 將mystr元素拷貝給mystr2
vector<string> mystr3 = mystr; // 將mystr元素拷貝給mystr3

(3)C++11標准中,用列表初始化方法給值,用{}括起來

vector<string> mystr4 = {“aaa”,”bbb”,”ccc”};

(4)創建指定數量的元素

// 創建15個int類型的元素,每個元素的值為-200
vector<int> ijihe(15,-200);
// 創建15個int類型的元素,每個元素的值為hello
vector<string> sjihe(15,”hello”);

vector<int> ijihe2(15); // 15個元素,每一個元素值默認為0
vector<string> sjihe(15); // 15個元素,每一個元素值默認為空

(5)多種初始化方式,()一般表示元素數量,{}一般表示元素內容這個概念,但不絕對。

想要正常的通過{}初始化,那么{}里面值的類型,必須要跟vector后面這個<>里面的元素類型相同。

vector<int> i1(10); // 表示創建10個元素,每個元素值為0
vector<int> i2{10}; // 表示創建一個元素,該元素的值為10
vector<string> snor{“hello”}; // 創建一個元素,元素內容為hello
vector<string> s22{10}; // 10個元素,每個元素為空
vector<string> s23{10,,”hello”}; // 10個元素,每一個元素內容為hello
vector<int> i3(10,1);//10個元素,每個元素值為1
vector<int> i4{10,1}; // 2個元素
//vector<int> i4{“hello”}; // 語法錯誤

三、vector對象上的操作

最常用的是不知道vector里面有多少個元素,需要動態的增加/減少。

一般情況下,先創建空的vector對象。

(1)判斷是否為空empty(),返回布爾值

vector<int> ivec;
if(ivec.empty())
{
    cout << “ivec為空” << endl;
}

(2)push_back():用於向vector中的末尾添加一個元素

ivec.push_back(1);
ivec.push_back(2);
for(int i = 3; i <= 100; i++)
{
    ivec.push_back(i);
}

(3)size():返回元素個數

cout << ivec.size() << endl;

(4)clear():移除所有元素,將容器清空

//ivec.clear();
//cout << ivec.size() << endl;

(5)ivec[n]:返回ivec中第n個元素(n是個整型值),位置從0開始,必須小於.size(),如果引用的下標超過這個范圍,或者用[]訪問了一個孔的vector,那么就會產生不可預測的結果,編譯器發現不了。

cout << ivec[1] << endl; // [0]:1 ,[1]:2

(6)= 賦值

vector<int> ivec2;
ivec2.push_back(111);
ivec2 = ivec; // ivec2得到了100個元素,ivec2原來的元素消失。
ivec2 = {12,13,14,15}; // 用{}的值取代了ivec2中原來的值
cout << ivec2.size() << endl;

(7)==,!= 相等,不相等

兩個vector相等,元素數量相等,對應位置元素值也一樣,否則就是不相等

int ivec3 = ivec2;
if(ivec3 == ivec2)
{
    cout << “ivec3 == ivec2” << endl;
}

ivec3.push_back(3);
if(ivec3 != ivec2)
{
    cout << “ivec3 != ivec2” << endl;
}

ivec3.clear();
ivec2.clear();
if(ivec3 == ivec2)
{
    cout << “ivec3 == ivec2” << endl;
}

(8)范圍for的應用

vector<int> vecvalue{1,2,3,4,5};
for(auto &vecitem : vecvalue) // 引用起到修改變量的作用
{
    vecitem *= 2;
}

for(auto vecitem : vecvalue)
{
    cout << vecitem << endl;   // 2,4,6,8,10
}

(8)范圍for的進一步理解

for(auto vecitem : vecvalue)
{
    //vecvalue.push_back(888); // 導致輸出徹底亂套
    cout << vecitem << endl;
}

在for語句中遍歷一個容器等等類似操作,千萬不要改動vector容器的容量,增加/刪除都不可以,否則會出問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM