当前位置:网站首页>STL resize容量规律集合
STL resize容量规律集合
2022-07-22 05:57:00 【joker_0030】
1、
#include <iostream>
#include<vector>
#include<list>
using namespace std;
void fun1()
{
string db(5, 'a');
cout << db.capacity() << endl;
db.resize(3);//规律:重新设置元素个数,若元素个数少于默认容量(默认容量为15,重新设置元素个数为3),则容量大小不变;若重新设置的元素个数大于容量则容量增大,如设置的容量大小为6,若重新设置元素个数为17,则容量大小为31。
cout << db << endl;
cout << db.capacity() << endl;
db.resize(17);//15 31 47 规律增加:15 16 16
cout << db.capacity() << endl;
vector<int> db2(6);
cout << db2.capacity() << endl;
db2.resize(3);//重新设置元素的个数,等于容量(如设置容量大小为6,重新设置元素个数为3),则容量大小不变;若重新设置的元素个数大于容量则容量增大,如设置的容量大小为6,若重新设置元素个数为7,则容量大小为6+6/2=9。
cout << db2.capacity() << endl;
db2.resize(7);
cout << db2.capacity() << endl;
list<int> db3(5);
cout << db3.size() << endl;
db3.resize(3);//重新设置元素个数,容量(如设置容量大小为5,重新设置元素个数为3),则容量大小为3。
cout << db3.size() << endl;
db3.resize(7);
cout << db3.size() << endl;
}
int main()
{
fun1();
system("pause");
return 0;
}
边栏推荐
- Under fitting and over fitting (regularization)
- Win11终端管理员打不开解决方法
- Tutorial update 20220719
- Activity recommendation | Apache pulsar's exploration and practice in vivo will be broadcast soon
- Write a sequencer plug-in sequence subtitle (1)
- 服务器buffer/cache 的产生原因和释放buffer/cache
- The difference between final, finally and finalize
- 牛客网 替换空格
- [machine learning] how pytorch loads custom datasets and divides them
- 【图文并茂】在线一键重装win7系统详细教程
猜你喜欢
vim入门
Qt5.9.2 initial import using msvc2017_ 64 record of problems encountered by compiler
Application of tensorflow optimizers in iris classification task
Opencv supports H264 video coding
keepalived
Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
Thread series coordination principle
UE4 key to open the door
UE4 create a project
注意力机制的分类
随机推荐
Is there anyone who can't analyze these data cases? A trick to teach you how to visualize recruitment data~
[reprint] UE4 interview Basics (II)
This article introduces you to the workflow of Redux - action/reducer/store
Overview of basic principles of network
【机器学习】pytorch如何加载自定义数据集并进行数据集划分
JSON_ Incorrect problem returned by extract
The difference between final, finally and finalize
Take it. The strongest "high concurrency" system design 46 consecutive questions, killing a crowd of interviewers every minute
Ora-16525 DG broker not available
Matlab function: filtfilter -- zero phase digital filtering
[red team] att & CK - browser extension for persistence
Principle and performance analysis of lepton lossless compression
一种跳板机的实现思路
Angr principle and Practice (I) -- principle
Under fitting and over fitting (regularization)
Oracle stored procedure parameter understanding
[web page performance optimization] - about lazy image loading
Use OpenCV to achieve the halo effect
What are the ways of configuring Apache virtual hosts
-bash-4.2$