当前位置:网站首页>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,
removing
those beyond (and freeing them). - If n is greater than the current container size, expand the content by
inserting
the 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
边栏推荐
- 快速使用UE4制作”大场景游戏“
- Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
- 自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
- Common layout effect realization scheme
- 华南师范宋宇老师课堂对话论文翻译
- js uses the string as the js execution code
- Redis deletes keys in batches according to regular rules
- LeetCode刷题第16天之《239滑动窗口最大值》
- 【小记】BatchSize的数值是设置的越大越好吗
- Dry goods: The principle and practice of server network card group technology
猜你喜欢
交换机--- 生成树--三层架构总结
0 Basic software test for career change, self-study for 3 months, 12k*13 salary offer
「转」“搜索”的原理,架构,实现,实践,面试不用再怕了
Three 】 【 yolov7 series of actual combat from 0 to build training data sets
What is machine learning?Explain machine learning concepts in detail
Get Qt installation information: including installation directory and various macro addresses
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
[C Language] Getting Started
Use jackson to parse json data in detail
【深度学习】基于卷积神经网络的天气识别训练
随机推荐
校园兼职平台项目反思
简历里写了会代码,却依然过不了面试这一关
Pinduoduo store business license related issues
快速使用UE4制作”大场景游戏“
Snap - rotate the smallest number of an array
Harvesting of radio frequency energy
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7
堆排序 和冒泡排序
What is machine learning?Explain machine learning concepts in detail
Redis deletes keys in batches according to regular rules
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
Listen to pull out U disk inserted into the message, U disk drive
Map中的getOrDefualt方法
洛谷P1196 银河英雄传说
源代码加密技术浅析
Events and scheduled tasks in Mysql
Switch---Spanning Tree---Three-layer Architecture Summary
Overview of the JVM garbage collection and mechanism
【yolov7系列三】实战从0构建训练自己的数据集
【ImageNet】数据集1000个类的名称