当前位置:网站首页>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边栏推荐
- .NET 服务注册
- LeetCode刷题第16天之《239滑动窗口最大值》
- 如何进行AI业务诊断,快速识别降本提效增长点?
- 【实战场景】商城-折扣活动设计方案
- 洛谷P6586 蒟蒻火锅的盛宴
- About data paging display
- Listen to pull out U disk inserted into the message, U disk drive
- Switch---Spanning Tree---Three-layer Architecture Summary
- Alibaba Cloud releases 3 high-performance computing solutions
- 1815. 得到新鲜甜甜圈的最多组数 状态压缩
猜你喜欢

"125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions

LeetCode刷题第16天之《239滑动窗口最大值》

对象的创建以及显示转换

"98 BST and Its Verification" of the 13th day of leetcode brushing series of binary tree series

0 Basic software test for career change, self-study for 3 months, 12k*13 salary offer

How to add icons to web pages?

LeetCode刷题第10天字符串系列之《125回文串验证》

Dry goods: The principle and practice of server network card group technology

网络安全培训机构哪家好?排名怎么选择?

Jetson Orin platform 4-16 channel GMSL2/GSML1 camera acquisition kit recommended
随机推荐
0基础转行软件测试,自学3个月,浅拿12k*13薪offer
【深度学习】基于卷积神经网络的天气识别训练
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
Snap - rotate the smallest number of an array
Selenium自动化测试框架工作原理你明白了吗?
一种基于共识机制的数字集群终端防失控方案研究
自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
二叉堆的基础~
Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
2022新员工公司级安全教育基础培训(118页)
CAN/以太网转换器 CAN与以太网互联互通
The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
Common layout effect realization scheme
es-head plugin insert query and conditional query (5)
map和set--天然的搜索和查找语义
Dry goods: The principle and practice of server network card group technology
(转)JVM中那些区域会发生OOM?
优先级队列
"239 Sliding Window Maximum Value" on the 16th day of LeetCode brushing
梅克尔工作室--OpenEuler培训笔记(1)