C++ Vector Resize函数[通俗易懂]

C++VectorResize函数ChangesizeResizesthecontainersothatitcontainsnelements.Ifnissmallerthanthecurrentcontainersize,thecontentisreducedtoitsfirstnelements,removingthosebeyond(anddestroyingthem).Ifnisgreaterthanthecu

大家好,又见面了,我是你们的朋友全栈君。

C++ Vector Resize函数

Change size

Resizes the container so that it contains n elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.

void resize (size_type n, value_type val = value_type());

改变vector容器的大小。

改变容器的大小使他包含 n n n个元素。

  • 如果 n n n 小于当前容器大小,则内容减少到前 n n n个元素,移除后面的元素。
  • 如果 n n n 大于当前容器大小,则在后面插入一些元素至 n n n个大小,如果 v a l val val被定义则插入 v a l val val的复制,否则插入默认值 0 0 0。
  • 如果 n n n大于当前容器的容量 ( c a p a c i t y ) (capacity) (capacity),则自动进行内容重新分配。

实例

// resizing vector
#include <iostream>
#include <vector>

int main ()
{ 
   
  std::vector<int> myvector;

  // set some initial content:
  for (int i=1;i<10;i++) myvector.push_back(i);

  myvector.resize(5);
  myvector.resize(8,100);
  myvector.resize(12);

  std::cout << "myvector contains:";
  for (int i=0;i<myvector.size();i++)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

结果

myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126842.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 免备案CDN推荐+教程

    免备案CDN推荐+教程文章目录前言一、CDN1.注册2.配置二、域名解析三、Nginx配置修改前言最近在搭建个人博客,到备案的时候发现困难重重(很麻烦),然后就想着用点其他法子。于是乎网上搜到了这个免备案CDN,花了一会部署完成后,感觉还不错就记录下来。附上我的博客链接:chasezc,功能还不是很完善,还在继续补充。一、CDN在网上查到的这个叫Nodecache,主要提供CDN、DNS、SSL证书等业务,亚太节点有我们喜欢的香港线路,速度很不错。1.注册首先我们去官网上注册:https://www.node

    2022年9月11日
  • 权重系数确定问题_如何确定权重系数例子

    权重系数确定问题_如何确定权重系数例子根据各个指标获得综合指标时,由于各个指标对综合指标的贡献度不同,相应权重也应不同,对综合指标贡献大的指标更重要,应该分配更大的权重。如何确定各个指标的权重,这里介绍两种方法:熵值法和pca确定权重。也

    2022年8月4日
  • 2.6 低音谱F谱表[通俗易懂]

    2.6 低音谱F谱表[通俗易懂]2.6 低音谱F谱表七音唱名倒念:tilasolfamiredo需要记。达到阅读五线谱像阅读文字那样。

    2022年8月5日
  • arduino超声波测距_stm32超声波测距lcd显示

    arduino超声波测距_stm32超声波测距lcd显示加入高工智能汽车行业群(自动驾驶行业4群,车联网智能座舱3群,智能商用车行业群),加微信:15818636852,并出示名片,仅限智能网联汽车零部件及OEM厂商。目前为止,特斯拉的Autopilot一共经历了三代硬件的更迭,分别是Autopilot1.0,2.0和2.5。按照目前特斯拉的公开信息,Autopilot3.0硬件将可能在今年底和自主研发的芯片一起推出。此前,《高工智能汽车》陆…

    2022年9月3日
  • mysql全文索引实现搜索功能(关键词查询)

    mysql全文索引实现搜索功能(关键词查询)最近在做一个关键词查询功能。所以开始了解mysql的全文索引技术。接下来我将一步一步告诉大家。我是如何一步一步实现关键词检索的。1.了解到mysql全文检索是以词为基础的。MySQL默认的分词是所有非字母和数字的特殊符号都是分词符。所以我存在数据库的样子是这样的。(左边的字段用于显示,右边的字段用于全文查询)2.全文检索的sqlSELECT*FROMtbk_item_coupon…

    2022年6月21日
  • freemarker中的round、floor和ceiling数字的舍入处理

    freemarker中的round、floor和ceiling数字的舍入处理

    2021年11月15日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号