如果std :: vector :: resize的size参数等于当前大小怎么办? [英] What if size argument for std::vector::resize is equal to the current size?

查看:59
本文介绍了如果std :: vector :: resize的size参数等于当前大小怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读有关vector :: resize > http://www.cplusplus的手册.com/reference/vector/vector/resize/

Reading the manual about vector::resize http://www.cplusplus.com/reference/vector/vector/resize/

它只说大小大于或等于小会发生什么,但不说大小相等会发生什么.是否保证在相等大小下不会重新分配数组并使迭代器无效?

It only says what happens if the size is greater or smaller, but does not say what happens if it's equal. Is it guaranteed that on equal size it will not reallocate the array and invalidate the iterators?

我想避免一个分支并且只处理2种情况(> =或<)而不是3种情况(<或>或==),但是如果未调整大小到相同大小,则我将不得不检查也是这样.

I wanted to avoid one branch and handle only 2 cases (>= or <) instead of 3 (< or > or ==), but if resizing to same size is undefined, then i will have to check that case too.

推荐答案

这可能只是链接参考中的错误.该标准说关注:

This is probably just an error in the linked reference. The standard says following:

void resize(size_type sz);

效果:如果 sz<size(),从序列中删除最后一个 size()-sz 元素.否则,将 sz-size()默认插入的元素追加到序列中.

Effects: If sz < size(), erases the last size() - sz elements from the sequence. Otherwise, appends sz - size() default-inserted elements to the sequence.

由于 sz-size()在您的情况下为 0 ,因此它不会执行任何操作.

Since sz - size() is 0 in your case, it doesn't do anything.

这篇关于如果std :: vector :: resize的size参数等于当前大小怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆