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

stl vector 函数

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

vector::size() is a library function of "vector" header, it is used to get the size of a vector, it returns the total number of elements in the vector.

vector :: size()“ vector”标头的库函数,用于获取矢量的大小,它返回矢量中元素的总数。

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

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

Syntax of vector::size() function

vector :: size()函数的语法

    vector::size();

Parameter(s): none – It accepts nothing.

参数: 无 –不接受任何内容。

Return value: size_type – It returns the size as an unsigned integral type.

返回值: size_type –它以无符号整数类型返回大小。

Example:

例:

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

    Function call:
    cout << vector1.size();

    Output:
    5

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

//C++ STL program to demonstrate example of
//vector::size() 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;

    //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;

    return 0;
}

Output

输出量

Total number of elements: 0
Total number of elements: 5

Reference: C++ vector::size()

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

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

stl vector 函数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值