vector :: clear()函数,以及C ++ STL中的示例
C++vector::clear()函数
vector::clear()是“vector”头文件的库函数,用于删除/清除矢量的所有元素,删除所有元素后将其大小设为0。
注意:要使用向量,请包含<vector>标头。
vector::clear()函数的语法
vector::clear();
参数:无–不接受任何内容。
返回值:void–不返回任何内容。
示例
Input:
vector<int> v1{ 10, 20, 30, 40, 50 };
//清除向量的内容
v1.clear();
cout <> v1.size();
Output:
0C++程序演示vector::clear()函数的示例
//C++STL程序演示示例
//vector::clear()函数
#include <iostream>
#include <vector>
using namespace std;
int main(){
//向量声明
vector<int> v1{ 10, 20, 30, 40, 50 };
//打印元素
cout << "before clearing the elements..." << endl;
cout << "size of v1: " << v1.size() << endl;
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
//清除所有元素
v1.clear();
//打印元素
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;
}输出结果
before clearing the elements... size of v1: 5 v1: 10 20 30 40 50 after clearing the elements... size of v1: 0 v1:
参考:C++vector::clear()
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短