C++清空容器

来源:百度知道 编辑:UC知道 时间:2024/06/17 09:58:04
请问如何清空容器中的元素?

// vector_clear.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
using namespace std;
vector <int> v1;

v1.push_back( 10 );
v1.push_back( 20 );
v1.push_back( 30 );

cout << "The size of v1 is " << v1.size( ) << endl;
v1.clear( );
cout << "The size of v1 after clearing is " << v1.size( ) << endl;
}

参考:MSDN

clear()就可以了