标签:style blog http color io os ar for sp
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#include <stdio.h>
#include <stdlib.h> #include <string.h> #include <vector> #include <iostream> using namespace std; /* iterator erase (const_iterator position); iterator erase (const_iterator first, const_iterator last); */ void print(const vector<int> &v) { vector<int>::const_iterator iter = v.begin(); for(; iter != v.end(); ++iter) cout << *iter << " "; cout << endl; } void test_case() { vector<int> v; for(int i = 1; i <= 10; i++) v.push_back(i); print(v); // erase 3 and 5 vector<int>::iterator iter; for(iter = v.begin(); iter != v.end();) { if(*iter == 3 || *iter == 5) { iter = v.erase(iter); } else { iter++; } } print(v); // erase 10 for(iter = v.begin(); iter != v.end();) { if(*iter == 10) { iter = v.erase(iter); } else { iter++; } } print(v); // erase from [first,last) iter = v.erase(v.begin(), v.begin() + 2); print(v); cout << *iter << endl; } int main() { test_case(); return 0; } /* 1 2 3 4 5 6 7 8 9 10 1 2 4 6 7 8 9 10 1 2 4 6 7 8 9 4 6 7 8 9 4 */ |
标签:style blog http color io os ar for sp
原文地址:http://www.cnblogs.com/hellogiser/p/vector_erase.html