stl vector 函数
C ++ vector :: clear()函数 (C++ vector::clear() function)
vector::clear() is a library function of "vector" header, it is used to remove/clear all elements of the vector, it makes the 0 sized vector after removing all elements.
vector :: clear()是“ vector”头文件的库函数,用于删除/清除向量中的所有元素,删除所有元素后将其大小设为0。
Note: To use vector, include <vector> header.
注意:要使用向量,请包含<vector>标头。
Syntax of vector::clear() function
vector :: clear()函数的语法
vector::clear();
Parameter(s): none – It accepts nothing.
参数: 无 –不接受任何内容。
Return value: void – It returns nothing.
返回值: void –不返回任何内容。
Example:
例:
Input:
vector<int> v1{ 10, 20, 30, 40, 50 };
//clearing content of the vectors
v1.clear();
cout <> v1.size();
Output:
0
C ++程序演示vector :: clear()函数的示例 (C++ program to demonstrate example of vector::clear() function)
//C++ STL program to demonstrate example of
//vector::clear() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1{ 10, 20, 30, 40, 50 };
//printing elements
cout << "before clearing the elements..." << endl;
cout << "size of v1: " << v1.size() << endl;
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
//clearing all elements
v1.clear();
//printing elements
cout << "after clearing the elements..." << endl;
cout << "size of v1: " << v1.size() << endl;
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
return 0;
}
Output
输出量
before clearing the elements...
size of v1: 5
v1: 10 20 30 40 50
after clearing the elements...
size of v1: 0
v1:
Reference: C++ vector::clear()
参考: C ++ vector :: clear()
翻译自: https://www.includehelp.com/stl/vector-clear-function-with-example.aspx
stl vector 函数