C++ std::vector::size()函数(返回向量中的元素数量)

cppman std::vector::size

std::vector::size(3)                                                  C++ Programmer's Manual                                                  std::vector::size(3)



NAME
       std::vector::size - Return size

TYPE
       public member function

SYNOPSIS
       #include <vector>


       C++98
       size_type size() const;
       size_type size() const noexcept;

DESCRIPTION
       Returns the number of elements in the vector.
       This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.
       //返回向量中的元素数量。
        //这是向量中保存的实际对象的数量,不一定等于它的存储容量。

PARAMETERS
       none

RETURN VALUE
       The number of elements in the container.
       Member type size_type is an unsigned integral type.
       //容器中的元素数量。
        //成员类型 size_type 是无符号整数类型。

EXAMPLE
         // vector::size
         #include <iostream>
         #include <vector>
         int main ()
         {
           std::vector<int> myints;
           std::cout << "0. size: " << myints.size() << '\n';
           for (int i=0; i<10; i++) myints.push_back(i);
           std::cout << "1. size: " << myints.size() << '\n';
           myints.insert (myints.end(),10,100);
           std::cout << "2. size: " << myints.size() << '\n';
           myints.pop_back();
           std::cout << "3. size: " << myints.size() << '\n';
           return 0;
         }

       Output:
         0. size: 0
         1. size: 10
         2. size: 20
         3. size: 19


COMPLEXITY
       Constant.

ITERATOR VALIDITY
       No changes.

DATA RACES
       The container is accessed.
       No contained elements are accessed: concurrently accessing or modifying them is safe.

EXCEPTION SAFETY
       No-throw guarantee: this member function never throws exceptions.

SEE ALSO
       vector::capacity(3)
              Return size of allocated storage capacity  (public member function)

       vector::resize(3)
              Change size  (public member function)

       vector::max_size(3)
              Return maximum size  (public member function)

REFERENCE
       cplusplus.com, 2000-2015 - All rights reserved.



cplusplus.com                                                                2022-05-13                                                        std::vector::size(3)
(END)

上面文档中的代码示例

#pragma warning(disable : 4996)
#include <iostream>
#include <vector>
int main()
{
    std::vector<int> myints;
    std::cout << "0. size: " << myints.size() << '\n';
    for (int i = 0; i < 10; i++) myints.push_back(i);
    std::cout << "1. size: " << myints.size() << '\n';
    myints.insert(myints.end(), 10, 100);   //向末尾插入10个值为100的元素
    std::cout << "2. size: " << myints.size() << '\n';
    myints.pop_back();
    std::cout << "3. size: " << myints.size() << '\n';
    return 0;
}

运行结果:

0. size: 0
1. size: 10
2. size: 20
3. size: 19
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个问题可以通过在内部向量上使用push_back()函数来解决。例如,假设我们有一个名为outerVec的std::vector<std::vector<double>>,我们想将一个名为innerVec的std::vector<double>添加到outerVec: ```c++ std::vector<double> innerVec = {1.0, 2.0, 3.0}; std::vector<std::vector<double>> outerVec; outerVec.push_back(innerVec); ``` 在这个例子,我们首先创建一个名为innerVec的std::vector<double>,并将其初始化为{1.0, 2.0, 3.0}。接下来,我们使用outerVec的push_back()函数将innerVec添加到outerVec。这将创建一个新的std::vector<double>,并将其添加到outerVec的末尾。 请注意,当我们将innerVec添加到outerVec时,它的值被复制到outerVec的新向量。这意味着如果我们稍后修改innerVec的值,它不会影响outerVec的值。如果我们想要在outerVec添加innerVec的引用,我们可以使用std::reference_wrapper,如下所示: ```c++ std::vector<double> innerVec = {1.0, 2.0, 3.0}; std::vector<std::reference_wrapper<std::vector<double>>> outerVec; outerVec.push_back(std::ref(innerVec)); ``` 在这个例子,我们首先创建一个名为innerVec的std::vector<double>,并将其初始化为{1.0, 2.0, 3.0}。接下来,我们创建一个std::vector<std::reference_wrapper<std::vector<double>>>,并使用std::ref()函数将innerVec添加到outerVec。这将创建一个std::reference_wrapper<std::vector<double>>,该引用包装器允许我们在outerVec添加对innerVec的引用。这意味着如果我们稍后修改innerVec的值,它将影响outerVec的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值