stl vector 函数_vector :: clear()函数,以及C ++ STL中的示例

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 函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值