1. 程式人生 > >C語言vector的erase

C語言vector的erase

看程式碼:

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

int main(){
    vector<int> v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(4);
    printf("Original vector:\n");
    for(int i = 0; i<v.size(); i++){
        printf("%d ", v[i]);
    }
    v.erase(v.begin() + 1
); printf("\nAfter erasing:\n"); for(int i = 0; i<v.size(); i++){ printf("%d ", v[i]); } }

所以輸出大概是:

Original vector:
1 3 4
After erasing:
1 4

就把第一個刪掉了哈哈