在此示例中,我创建了一个包含一个整数的向量,然后从向量中删除该整数。向量的大小减小了,但整数仍然存在!为什么整数还在?大小为 0 的向量如何包含元素?
#include <vector>
#include <iostream>
using namespace std;
int main(int agrc, char* argv[])
{
vector<int> v;
v.push_back(450);
cout << "Before" << endl;
cout << "Size: " << v.size() << endl;
cout << "First element: " << (*v.begin()) << endl;
v.erase(v.begin());
cout << "After" << endl;
cout << "Size: " << v.size() << endl;
cout << "First element: " << *(v.begin()) << endl;
return(0);
}
输出:
Before
Size: 1
First element: 450
After
Size: 0
First element: 450