C++ Vector fundamental

小明同学写博客發表於2024-04-23

C++ Vector fundamental

主要內容包括: 引入標頭檔案,如何建立並初始化,訪問容量,增查刪改;

1. 包含標頭檔案

#include <vector>
#include <iostream>

2. 建立 vector

vector<int> v;
vector<char> v1;
vector<string> v2;

3. 初始化一維 vector

初始化後不進行賦值,直接訪問會報錯;

3.1 vector v

vector <int> v;
v.resize(5);	// 使用 5 個 0 來初始化

for (int i=0; i < v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;
vector<int> v1;
v1.resize(5, 1);	// 使用 5 個 1 來初始化

for (int i=0; i < v1.size(); i++){
    cout << v1[i] << ' ';
}
cout << endl;

3.2 vector v=

vector<int> v = {1, 2, 3, 4, 5};
v.resize(3);	// 丟棄元素 4 和 5
// 1, 2, 3
for(int i=0; i<v.size(); i++){
    cout << v[i] << ' ';
}
cout << endl;
vector<int> v1 = {1, 2, 3, 4, 5};
v1.resize(7);	// 填充0
// 1, 2, 3, 4, 5, 0, 0
for (int i=0; i < v1.size(); i++){
    cout << v1[i] << ' ';
}
cout << endl;

3.3 copy vector

vector<int> testVector = {1, 2, 3, 4, 5};
vector<int> v(testVector);
// 1, 2, 3, 4, 5
for(int i=0; i<v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;
vector<int> testVector = {1, 2, 3, 4, 5};
vector<int> v = testVector;

for (int i=0; i<v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;

3.4 pointer init vector

	int arr[5] = {1, 2, 3, 4, 5};
    vector<int> v = {1, 2, 3, 4};

    // 1, 2, 3
    vector<int> v1(arr, arr+3);
    for (int i =0; i < v1.size(); i++){
        cout << v1[i] << " ";
    }
    cout << endl;

    // 2, 3
    vector<int> v2(v.begin()+1, v.end()-1);
    for(int i=0; i<v2.size(); i++){
        cout << v2[i] << " ";
    }
    cout << endl;

4. 初始化二維 vector

初始化後不進行賦值,直接訪問會報錯

4.1 vector<vector > v;

vector< vector<int>>v;
v.resize(5);
for (int i=0; i<5; i++){
    v.resize(5);
}

// all output si 0
for(int i=0; i<v.size();i++){
    for (int j=0; j<v[i].size();j++){
        cout << v[i][j] << " ";
    }
    cout << endl;
}
cout << endl;

4.2 vector <vector> v(n, testVctor)

vector <int> testVector(4, 1);
vector < vector<int> > v(4, testVector);

// 4*4 output is 1
for(int i=0; i<v.size(); i++){
    for(int j=0; j<v[i].size();j++){
        cout << v[i][j] << " ";
    }
    cout << endl;
}
cout << endl;

4.3 pointer init vector

// vector 的指標
vector <int> v1 = {1, 2, 3, 4};
vector< vector<int> > v2(4, v1);

/* 1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
*/
vector< vector<int> > v3(v2.begin(), v2.end());
for(int i=0; i<v3.size(); i++){
    for(int j=0; j<v3[i].size(); j++){
        cout << v3[i][j] << " ";
    }
    cout << endl;
}
cout << endl;
// 普通陣列的指標
int arr[4][4] = {
    {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}
};

vector<vector <int>> v;
for(int i=0; i<4; i++){
    vector<int> row(arr[i], arr[i]+4);
    v.push_back(row);
}

for(int i=0; i<v.size(); i++){
    for(int j=0; j<v[i].size(); j++){
        cout << v[i][j] << ' ';
    }
    cout << endl;
}
cout << endl;

5. 直接訪問 vector 中的元素

vector<int> v = {100, 200, 300, 400};

cout << v[0] << endl;
cout << v[1] << endl;
cout << v[2] << endl;
cout << v[3] << endl;

6. 獲取 vector 大小

vecotr <int> v = {100, 200, 300, 400, 500};

cout << v.size() << endl;
cout << v.capacity() << endl;

7. 向 vector 新增元素

vector<int> v = {100, 200, 300};

// 向末尾新增元素 
v.push_back(1);
v.push_back(2);
v.push_back(3);

for(int i=0; i<v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;

8. 向 vector 插入元素

vector <int> v1 = {100, 200, 300, 400, 500, 600};
vector <int>::iterator it;

it = v1.begin();
v1.insert(it, 111);
// out: 111, 100, 200, 300, 400, 500, 600
for(int i=0; i<v1.size(); i++){
    cout << v1[i] << " ";
}
cout << endl;

it = v1.begin() + 2;
v1.insert(it, 222);
// out: 111, 100, 222, 200, 300, 400, 500, 600
for(int i=0; i<v1.size(); i++){
    cout << v1[i] << " ";
}
cout << endl;

it = v1.end();
v1.insert(it, 999);
for(int i=0; i<v1.size(); i++){
    cout << v1[i] << " ";
}
cout << endl;

9. 刪除 vector 中的元素:pop_back

vector<int> v = {100, 200, 300, 400. 500};

v.pop_back();	// 刪除末尾元素
v.pop_back();

cout << v.size() << endl;
for(int i=0; i<v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;

10. 刪除 vector 指定位置的元素: erase

vector<int> v = {1, 2, 3, 4, 5, 6};
vector<int>::iterator it;

it = v.begin();
v.erase(it);
// out: 2, 3, 4, 5, 6
for(int i=0; i<v.size();i++){
    cout << v[i] << " ";
}
cout << endl;

it = v.begin() + 2;
v.erase(it);
//out: 2, 3, 5, 6
for(int i=0; i<v.size();i++){
    cout << v[i] << " ";
}
cout << endl;

11. 刪除 vector 制定數值的元素:remove

#include <algorithm>

vector<int> v = {100, 200, 300, 400, 500, 600};

v.erase(remove(v.begin(), v.end(), 500), v.end());
v.erase(remove(v.begin(), v.end(), 300), v.end());

// out: 100, 200, 400, 600
for(int i=0; i < v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;
#include <algorithm>

vector <int> v = {100, 200, 300, 400, 500, 600};

v.erase(remove(v.begin(), v.end(), 555), v.end());	// 找不到值,不執行任何操作
v.erase(remove(v.begin(), v.end(), 333), v.end());

for(int i=0; i<v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;

12. 修改 vector 中的元素


vector<int> v = {100, 200, 300, 400, 500, 600};
v[0] = 111;
v[1] = 222;

for(int i=0; i< v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;

13. 查詢 vector 中的元素

#include <algorithm>

vector<int> v = {100, 200, 300, 400, 500};
vector<int>::iterator it = find(v.begin(), v.end(), 500);

if (it != v.end()){
    cout << "index: " << distance(v.begin(), it) << endl;
}
else{
    cout << "not found" << endl;
}
#include <algorithm>

vector<int> v = {100, 200, 300, 400, 500, 600};
bool found = false;
int valueToFind = 300;

for(vector<int>::iterator it=v.begin(); it!=v.end(); ++it){
    if (*it == valueToFind){
        cout << "index: " << distance(v.begin(), it) << endl;
        found = true;
        break;
    }
}
if(!found){
    cout << "not found" << endl;
}

14. 清空 vector 中的元素:clear

vector<int> v = {100, 200, 300, 400, 500, 600};

cout << v.size() << endl;	//out: 6
v.clear();
cout << v.size() << endl;	//out: 0

15. 索引遍歷 vector 元素

vector <int> v = {100, 200, 300, 400, 500};
for(int i=0; i<v.size(); i++){
    cout << v[i] << " ";
}
cout << endl;

16. 迭代器遍歷 vector 元素

vector <int> v = {100, 200, 300, 400, 500};

for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
    cout << *it << " ";
}
cout << endl;

17. 使用 foreach 迴圈遍歷

vector <int> v = {100, 200, 300, 400, 500};
// auto it 是自動型別推斷,直接取得元素值
for(auto it:v){
    cout << it << " ";
}
cout << endl;

相關文章