// vector 的删除操作 #include using namespace std; template void print(vector &v) { typename vector::iterator it = v.begin(); while (it != v.end()) { cout << *it << " "; it++; } cout << endl; } int main() { int arr1[5] = {1, 2, 3, 4, 5}; vector v1(arr1, arr1 + 5); print(v1); vector::iterator it = v1.begin(); v1.insert(it, 1, 10); // 在 it 位置插入 1 个 10 print(v1); v1.resize(0); // 缩小 vector(v1).swap(v1); // 重新分配内存 print(v1); cout << "size: " << v1.size() << " capacity: " << v1.capacity() << endl; // size: 2 capacity: 8 return 0; }