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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DispatcherServlet contextConfigLocation

    DispatcherServlet contextConfigLocation//ClassPathXmlApplicationContext是读取src目录下的配置文件ApplicationContextapp=newClassPathXmlApplicationContext("applicationContext.xml");//FileSystemXmlApplicationContext即系统文件路径,文件的目录。Applica…

    2022年7月12日
    14
  • MFC界面库BCGControlBar的介绍

    MFC界面库BCGControlBar的介绍英文原文:http://www.bcgsoft.com/bcgcontrolbarpro.htmBCGControlBar是MFC的一个扩展库其英文全称是”BusinessComponentsGalleryControlBar”,它允许你去创建像完全自定义的像MicrosoftOffice2000/XP/2003/2007/2010/2013and VisualStudio的界

    2022年7月14日
    30
  • python 正则 括号_js正则匹配0个或多个空格

    python 正则 括号_js正则匹配0个或多个空格\d匹配一个数字\w匹配一个字母或数字.匹配任意字符*表示任意个字符(包括0个),+表示至少一个字符?表示0个或1个字符^表示行的开头$表示行的结束\s匹配一个空格(也包括Tab等空白符)\s+表示至少有一个空格|A|B可以匹配A或B,所以(P|p)ython可以匹配’Pyth…

    2025年6月3日
    0
  • PyCharm+SVN「建议收藏」

    PyCharm+SVN「建议收藏」首先电脑安装svn,并且确svn/bin下面有svn.exe文件没有bin/svn.exe解决方法:重新打开TortoiseSVN安装文件-Modify-Next后在commandlineclienttools选项修改为Willbeinstalledonlocalharddrive,等待安装完成,SVN目录会出现svn.exe文件如果以上解决…

    2022年8月29日
    1
  • ipv6双向网关_IPv4_IPv6转换网关·····[通俗易懂]

    IPV4/IPV6转换网关的研究与设计摘要:随着计算机网络应用的飞速进步,现有的IP通信协议(IPv4协议)已展现出众多的问题,如不能适应新的网络应用、地址资源即将耗尽以及对安全性无法保证等。IPv6是继IPv4后出现的新一代通信协议,它的出现为互联网的发展带来了新契机。IPv6的众多优势成为取代IPv4必然的发展。本文从IPv6协议本身出发,阐述了IPv6协议及其与IPv4协议的比较,对目前现有…

    2022年4月9日
    60
  • postgresql数据库报“connections on Unix domain socket “/tmp/.s.PGSQL.5432”?”

    postgresql数据库报“connections on Unix domain socket “/tmp/.s.PGSQL.5432”?”使用postgresql数据库的时候经常遇到的问题:[postgres@test~]$psqlpsql:couldnotconnecttoserver:Nosuchfileordirectory Istheserverrunninglocallyandaccepting connectionsonUnixdomainsocket"/tmp/.s…

    2022年6月19日
    121

发表回复

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

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