1. 通過 push_back() 尾部增加一個元素 :
vector 可以通過 “push_back ” 寫入數據,通過 push_back 可以將數據直接寫入至 vector 的末尾,push_back 會自動申請內存,並且多次 push_back 后會自動預先分配內存,如下代碼:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 //第一部分:容器 vector 9 vector<int> num; 10 11 cout << "num 的元素個數:" << num.size() << endl; 12 cout << "num 容器的大小:" << num.capacity() << endl; 13 14 num.push_back(1); //push_back:往 vector 最后放置1個元素 “1” 15 num.push_back(2); 16 num.push_back(3); 17 num.push_back(4); 18 num.push_back(3); 19 20 cout << "尾部插入5個元素后" << endl; 21 cout << "num 的元素個數:" << num.size() << endl; 22 cout << "num 容器的大小:" << num.capacity() << endl; 23 24 return 0; 25 }
運行結果:
插入5個元素后打印內存大小,結果發現這時的 vector 占用了6個 int 元素的內存
2.通過 pop_back() 刪除尾部的一個元素:
vector 可以通過 “pop_back” 刪除尾部一個元素,pop_back 刪除尾部的元素后,元素個數會變少,但空間並不會縮小,如下代碼:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(5, 888); 9 10 printf("使用 resize 之前\n"); 11 cout << "使用 resize 之前 num 的元素數量:" << num.size() << endl; 12 cout << "使用 resize 之前 num 的空間大小:" << num.capacity() << endl; 13 14 for (int i = 0; i < num.size(); i++) 15 { 16 cout << num[i] << endl; 17 } 18 19 num.pop_back(); 20 21 cout << "使用 resize 之后 num 的元素數量:" << num.size() << endl; 22 cout << "使用 resize 之后 num 的空間大小:" << num.capacity() << endl; 23 for (int i = 0; i < num.size(); i++) 24 { 25 cout << num[i] << endl; 26 } 27 28 return 0; 29 }
打印結果:
3. 使用下標進行數據修改:
如果使用下標進行數據寫入,需要一個前提,那就是 vector 已經進行了內存分配,如果像下方代碼:vector 沒有進行帶參構造,這時不能直接通過下標去寫 vector 的數據。
1 vector<int> num; 2 3 num[0] = 1; //不能這樣通過下標去訪問,因為vector是進行默認構造的,這時並沒有相關內存
以下幾種帶參構造的 vector 是可以進行下標數據寫入的:
//例1 int main() { vector<int> num(10); num[0] = 1; num[1] = 2; return 0; } //例2 int main() { vector<int> num(10); vector<int>num_1(num); num_1[0] = 1; num_1[1] = 2; return 0; } //例3 int main() { int test[] = { 1,2,3,4,5 }; vector<int> num(test, test + 2); num[0] = 1; num[1] = 2; return 0; }
4. 使用 vectorName.at() 進行數據修改:
這種用法與下標修改類似,at() 這個方法返回的是一個引用,可以吧一個常量 int 賦值給它,如下代碼:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(5, 111); 9 10 printf("使用 resize 之前\n"); 11 cout << "使用 resize 之前 num 的元素數量:" << num.size() << endl; 12 cout << "使用 resize 之前 num 的空間大小:" << num.capacity() << endl; 13 14 for (int i = 0; i < num.size(); i++) 15 { 16 cout << num[i] << endl; 17 } 18 19 num.at(1) = 222; 20 num.at(2) = 333; 21 num.at(3) = 444; 22 23 cout << "使用 resize 之后 num 的元素數量:" << num.size() << endl; 24 cout << "使用 resize 之后 num 的空間大小:" << num.capacity() << endl; 25 for (int i = 0; i < num.size(); i++) 26 { 27 cout << num[i] << endl; 28 } 29 30 return 0; 31 }
打印結果:
5. 使用 assign 進行重寫操作
assign 可以改變原來的 vector 中的元素個數和值,如下代碼:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(10, 666); 9 10 printf("使用 assign 之前\n"); 11 cout << "使用 assign 之前 num 的元素數量:" << num.size() << endl; 12 cout << "使用 assign 之前 num 的空間大小:" << num.capacity() << endl; 13 14 num[0] = 1; 15 num[1] = 2; 16 17 for (int i = 0; i < num.size(); i++) 18 { 19 cout << num[i] << endl; 20 } 21 num.assign(2,888); //第一種 assign 的用法 22 printf("使用 assign 之后\n"); 23 cout << "使用 assign 之后 num 的元素數量:" << num.size() << endl; 24 cout << "使用 assign 之后 num 的空間大小:" << num.capacity() << endl; 25 for (int i = 0; i < num.size(); i++) 26 { 27 cout << num[i] << endl; 28 } 29 30 return 0; 31 }
打印結果:
我們會發現,使用 assign 之后元素數量變為了2,但 vector 的空間大小並沒有變。
assign 還可以搭配迭代器用:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 vector<int>num(10, 111); 9 vector<int>num_1(10, 888); 10 11 printf("使用 assign 之前\n"); 12 cout << "使用 assign 之前 num_1 的元素數量:" << num_1.size() << endl; 13 cout << "使用 assign 之前 num_1 的空間大小:" << num_1.capacity() << endl; 14 15 num_1[0] = 1; 16 num_1[1] = 2; 17 18 for (int i = 0; i < num_1.size(); i++) 19 { 20 cout << num_1[i] << endl; 21 } 22 num_1.assign(2,888); //第一種 assign 的用法 23 num_1.assign(num.begin() + 3, num.end()); //第二種 配合迭代器的用法 24 25 printf("assign 的第二種用法:\n"); 26 cout << "assign 的第二種用法之后 num_1 的元素數量:" << num_1.size() << endl; 27 cout << "assign 的第二種用法之后 num_1 的空間大小:" << num_1.capacity() << endl; 28 for (int i = 0; i < num_1.size(); i++) 29 { 30 cout << num_1[i] << endl; 31 } 32 33 return 0; 34 }
打印如下:
既然使用迭代器可以這樣玩,那么使用數組也一樣可以,如下:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int test[] = { 111,222,333,444,555,666,777,888,999 }; 9 vector<int>num(10, 888); 10 11 printf("使用 assign 之前\n"); 12 cout << "使用 assign 之前 num_1 的元素數量:" << num.size() << endl; 13 cout << "使用 assign 之前 num_1 的空間大小:" << num.capacity() << endl; 14 15 num[0] = 1; 16 num[1] = 2; 17 18 for (int i = 0; i < num.size(); i++) 19 { 20 cout << num[i] << endl; 21 } 22 num.assign(2,888); //第一種 assign 的用法 23 num.assign(test, test + 5); //第二種 使用迭代器一樣的用法 用數組進行重新賦值 24 25 printf("assign 的第二種用法:\n"); 26 cout << "assign 的第二種用法之后 num_1 的元素數量:" << num.size() << endl; 27 cout << "assign 的第二種用法之后 num_1 的空間大小:" << num.capacity() << endl; 28 for (int i = 0; i < num.size(); i++) 29 { 30 cout << num[i] << endl; 31 } 32 33 return 0; 34 }
打印如下:
6. 獲取&修改 vector 容器的第一個和最后一個元素的值:
獲取:使用 vectorName.front() 與 vectorName.back() 來獲取 vector 的第一個元素與最后一個元素的引用,如下代碼:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int test[] = { 111,222,333,444,555,666,777,888,999 }; 9 vector<int>num(test, test + 9); 10 11 cout << "num 的第一個元素為:" << num.front() << endl; 12 cout << "num 的第一個元素為:" << num.back() << endl; 13 14 return 0; 15 }
打印結果:
賦值:因為返回的是其引用,那么我們也可以進行賦值操作,如下代碼:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int test[] = { 111,222,333,444,555,666,777,888,999 }; 9 vector<int>num(test, test + 9); 10 11 cout << "num 的第一個元素為:" << num.front() << endl; 12 cout << "num 的第一個元素為:" << num.back() << endl; 13 14 cout << "=========改變首尾的值========" << endl; 15 num.front() = 1; 16 num.back() = 9; 17 18 for (int i = 0; i < num.size(); i++) 19 { 20 cout << num.at(i) << endl; 21 } 22 23 return 0; 24 }
打印結果:
=======================================================================================================================