13、vector容量和大小

#include <iostream>
#include <vector>

using namespace std;

void printvector (vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it ++)
    {
        cout << *it << " ";
    }

    cout << endl;
}

void test01 ()
{
    vector<int> v1;

    for (int i = 0; i < 10; i ++)
    {
        v1.push_back(i);
    }

    printvector(v1);

    if (v1.empty())
    {
        cout << "v1 is empty" << endl;
    }
    else
    {
        cout << "v1 is not empty" << endl;

        cout << "v1's capacity is " << v1.capacity() << endl;
        cout << "v1's size is " << v1.size() << endl;
    }

    //重新指定大小
    v1.resize(16, 100);    //利用重载的版本,可以指定默认填充值,参数2
    printvector(v1);    //如果重新指定的比原来长,默认用0填充

    v1.resize(5);
    printvector(v1);    //超出部分被截取
}

int main ()
{
    test01();

    return 0;
}

 

相关推荐

  1. C++学习-2023/12/11-2.vector容器

    2024-03-12 04:38:01       39 阅读
  2. vector容器

    2024-03-12 04:38:01       22 阅读
  3. resizereverse修改容器大小

    2024-03-12 04:38:01       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-12 04:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-12 04:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-12 04:38:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-12 04:38:01       20 阅读

热门阅读

  1. 开发指南002-前后端信息交互规范-返回值定义

    2024-03-12 04:38:01       25 阅读
  2. 常用的推荐算法

    2024-03-12 04:38:01       19 阅读
  3. ARM TrustZone技术介绍

    2024-03-12 04:38:01       18 阅读
  4. linux新一代的RPM软件包管理器dnf

    2024-03-12 04:38:01       27 阅读
  5. Linux中basename作用

    2024-03-12 04:38:01       24 阅读
  6. Dutree:Linux 文件系统磁盘使用追踪工具

    2024-03-12 04:38:01       20 阅读