当前位置:网站首页>Resize() usage row pit in vector
Resize() usage row pit in vector
2022-08-11 04:42:00 【Darchan】
resize() usage in vector
Like first and then watch, and develop a good habit.If it helps, please follow!I will keep updating, thank you for your support!
Reference:
std::vector::resize
Requirements: The debugger has a problem that the vector is set to 0. Finally, it is located that the resize is used improperly, which is recorded.
Simplified description of the situation
1. Initialize the vector, and then want to use resize() to assign values to all elements
vector test = {0,1,2,3,4};test.resize(10,0);// print the resultfor(const auto &value: test) std::cout << value << ",";std::cout << std::endl; It is usually thought that the print result is 10 0s, but it is actually 0,1,2,3,4,0,0,0,0,0,
2. resize() function description
2.1 Function Prototype (C++11)
void resize (size_type n);void resize (size_type n, const value_type& val);2.2 official explanation
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 arevalue-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.2.3 Instructions
- Resize the container to contain n elements
- If n is less than the current container size, the content will be reduced to its first n elements,
removingthose beyond (and freeing them). - If n is greater than the current container size, expand the content by
insertingthe desired number of elements at the end to reach the size of n.If val is specified, initialize new elements to copies of val, otherwise, initialize them to 0. - If n is also greater than the current container capacity, the allocated storage is automatically reallocated.
Note that this function changes the actual content of the container by inserting or removing elements.
2.4 Practical Examples
Borrowing the official example.
// resizing vector#include #include int main(){std::vector 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 Results:
myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0
Explanation:
- Initialize 9 elements, from 1 to 9.
- The first operation, resize(5), deletes the last 4 elements, leaving 1 2 3 4 5.
- The second operation, resize(8,100), will insert 3 elements at the end, and the value of 3 elements is 100.
- The third operation, resize(12), will insert 12-8=4 elements at the end again. Since the element does not provide an initial value, the default value is 0.
2.5 Extensions
The real requirement in the project is to set all elements in the member variable vector to 0 at the beginning of the program, and finally use the std::fill() fill function, clear() will clear the value, but not free the memory.
std::fill(input_data_.begin(), input_data_.end(), 0); // fillinput_data_.clear(); // After clearing, traversing and printing will not see the result边栏推荐
- ALSA音频架构 -- aplay播放流程分析
- LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
- The basics of binary heap~
- 梅克尔工作室--OpenEuler培训笔记(1)
- I wrote some code in my resume, but I still can't pass the interview
- 2022新员工公司级安全教育基础培训(118页)
- 洛谷P7441 Erinnerung
- ALSA音频架构
- [Server installation Redis] Centos7 offline installation of redis
- Provincial level of Echart maps, as well as all prefecture-level download and use
猜你喜欢

视觉任务种常用的类别文件之一json文件

我的LaTeX入门
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

WPF DataGrid 使用数据模板(2)

洛谷P2150 寿司晚宴

Get Qt installation information: including installation directory and various macro addresses

【组成原理 九 CPU】

ALSA音频架构

Jetson Orin platform 4-16 channel GMSL2/GSML1 camera acquisition kit recommended

Switch---Spanning Tree---Three-layer Architecture Summary
随机推荐
WPF DataGrid 使用数据模板(2)
洛谷P1763 埃及分数
ALSA音频架构 -- snd_pcm_open函数分析
【力扣】22.括号生成
.NET service registration
(转)JVM中那些区域会发生OOM?
【深度学习】基于卷积神经网络的天气识别训练
Introduction to c # a week of high-level programming c # - LINQ Day Four
[Server installation Redis] Centos7 offline installation of redis
洛谷P5139 z小f的函数
使用百度EasyDL实现智能垃圾箱
The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
LeetCode刷题第10天字符串系列之《125回文串验证》
Redis deletes keys in batches according to regular rules
es-head plugin insert query and conditional query (5)
leetcode刷题第13天二叉树系列之《98 BST及其验证》
如何将360全景图导出高清短视频分享到视频平台上?
2022新员工公司级安全教育基础培训(118页)
如何缓解压力、拒绝内耗【1】
洛谷P2150 寿司晚宴