当前位置:网站首页>vector中resize() 用法排坑
vector中resize() 用法排坑
2022-08-11 04:32:00 【Darchan】
vector中resize() 用法排坑
先赞后看,养成好习惯。有帮助的话,点波关注!我会坚持更新,感谢谢您的支持!
需求: 调试程序出现一个vector置0的问题,最后定位到是resize使用不当,以此记录。
情况简化说明
1. 初始化vector,然后想用resize()为所有元素赋值
vector<int> test = {0,1,2,3,4};
test.resize(10,0);
// 打印结果
for(const auto &value: test) std::cout << value << ",";
std::cout << std::endl;
通常以为打印结果为10个0, 但是实际为0,1,2,3,4,0,0,0,0,0,
2. resize()函数说明
2.1 函数原型(C++11)
void resize (size_type n);
void resize (size_type n, const value_type& val);
2.2 官方解释
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.
2.3 用法说明
- 调整容器大小,使其包含n个元素
- 如果 n 小于当前容器大小,则内容将减少到它的前 n 个元素,
删除超出的那些(并释放它们)。 - 如果 n 大于当前容器大小,则通过在末尾
插入所需数量的元素来扩展内容,以达到 n 的大小。 如果指定了 val,则将新元素初始化为 val 的副本,否则,将它们初始化为0。 - 如果 n 也大于当前容器容量,则会自动重新分配已分配的存储空间。
注意,此函数通过插入或删除元素来更改容器的实际内容。
2.4 实践举例
借用官方的例子。
// 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
解释:
- 初始化9个元素,从1到9。
- 第一个操作resize(5),会删除最后的4个元素,此时剩下1 2 3 4 5。
- 第二个操作resize(8,100),会在尾部插入3个元素,3个元素值为100。
- 第三个操作resize(12),会再次再尾部插入12-8=4个元素,元素因为没有提供初始值,故采用默认的0。
2.5 拓展
项目中的真实需求是在程序开始位置,每次将成员变量vector中的所有元素置为0,最终采用std::fill() 填充函数,clear()会清空数值,但是不释放内存。
std::fill(input_data_.begin(), input_data_.end(), 0); // 填充
input_data_.clear(); // 清空后,遍历打印则看不到结果
边栏推荐
- "3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing
- 移动端地图开发选择哪家?
- uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
- send_sig: 内核执行流程
- 利用Navicat Premium导出数据库表结构信息至Excel
- How to add icons to web pages?
- LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
- 力扣——旋转数组的最小数字
- 如何将360全景图导出高清短视频分享到视频平台上?
- map和set--天然的搜索和查找语义
猜你喜欢

"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series

【深度学习】基于卷积神经网络的天气识别训练
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

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

北湖区燕泉街道开展“戴头盔·保安全”送头盔活动

leetcode刷题第13天二叉树系列之《98 BST及其验证》

Overview of the JVM garbage collection and mechanism

2022新员工公司级安全教育基础培训(118页)

LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"

获取Qt的安装信息:包括安装目录及各种宏地址
随机推荐
交换机--- 生成树--三层架构总结
Harvesting of radio frequency energy
Snap - rotate the smallest number of an array
解决多线程调用sql存储过程问题
Word2021 中的图片保存后就变模糊了
"239 Sliding Window Maximum Value" on the 16th day of LeetCode brushing
优先级队列
Overview of the JVM garbage collection and mechanism
[Web3 series development tutorial - create your first NFT (9)] How to view your NFT in the mobile wallet
【深度学习】基于卷积神经网络的天气识别训练
【实战场景】商城-折扣活动设计方案
.NET Custom Middleware
洛谷P4324 扭动的回文串
洛谷P6586 蒟蒻火锅的盛宴
获取Qt的安装信息:包括安装目录及各种宏地址
使用jackson解析json数据详讲
机器学习是什么?详解机器学习概念
【ImageNet】数据集1000个类的名称
Use jackson to parse json data in detail
利用Navicat Premium导出数据库表结构信息至Excel