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

stl vector 函数

C ++ vector :: empty()函数 (C++ vector::empty() function)

vector::empty() is a library function of "vector" header, it is used to check whether a given vector is an empty vector or not, it returns a true if the vector size is 0, otherwise it returns false.

矢量::空()“载体”报头的库函数,它是用来检查给定的矢量是否是一个空的载体或不是,它如果矢量大小为0,则返回 ,否则返回

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::empty() function

vector :: empty()函数的语法

    vector::empty();

Parameter(s): void – It accepts nothing as a parameter.

参数: void –不接受任何参数。

Return value: bool – It returns true if the vector size is 0, otherwise it returns false.

返回值: 布尔 -如果矢量大小为0,则返回true,否则返回false。

Example:

例:

    Input:
    vector<int> vector1{ 1, 2, 3, 4, 5 };
    vector<int> vector2;

    Function call:
    cout << vector1.empty() << endl;
    cout << vector2.empty() << endl;

    Output:
    false
    true

C ++程序演示vector :: empty()函数的示例 (C++ program to demonstrate example of vector::empty() function)

//C++ STL program to demonstrate example of
//vector::empty() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    //printing the size of the vector
    cout << "Total number of elements: " << v1.size() << endl;
    //checking whether vector is empty or not
    if (v1.empty())
        cout << "vector is empty." << endl;
    else
        cout << "vector is not empty." << endl;

    //pushing elements
    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    //printing the size of the vector
    cout << "Total number of elements: " << v1.size() << endl;
    //checking whether vector is empty or not
    if (v1.empty())
        cout << "vector is empty." << endl;
    else
        cout << "vector is not empty." << endl;

    return 0;
}

Output

输出量

Total number of elements: 0
vector is empty.
Total number of elements: 5
vector is not empty.

Reference: C++ vector::empty()

参考: C ++ vector :: empty()

翻译自: https://www.includehelp.com/stl/vector-empty-function-with-example.aspx

stl vector 函数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值