Edited after comment underscore_d.
Description of functions implemented in VS2015
VS2015 CTP6
This error dialog box only exists in DEBUG mode when #if _ITERATOR_DEBUG_LEVEL == 2 defined. In RELEASE mode, we have no problems. We get the current value of return (*(this->_Myfirst() + _Pos) , so the size value is not required:
reference operator[](size_type _Pos) {
If we see the vector in the source code, we can find that the difference between resize and reserve is only in changing the value of this->_Mylast() to func resize() .
reserve() calls _Reallocate .
resize() calls _Reserve , which calls _Reallocate , and then resize() also changes the value of this->_Mylast() : this->_Mylast() += _Newsize - size(); used in calculating size (see last function)
void resize(size_type _Newsize) { // determine new length, padding as needed if (_Newsize < size()) _Pop_back_n(size() - _Newsize); else if (size() < _Newsize) { // pad as needed _Reserve(_Newsize - size()); _TRY_BEGIN _Uninitialized_default_fill_n(this->_Mylast(), _Newsize - size(), this->_Getal()); _CATCH_ALL _Tidy(); _RERAISE; _CATCH_END this->_Mylast() += _Newsize - size(); } } void reserve(size_type _Count) { // determine new minimum length of allocated storage if (capacity() < _Count) { // something to do, check and reallocate if (max_size() < _Count) _Xlen(); _Reallocate(_Count); } } void _Reallocate(size_type _Count) { // move to array of exactly _Count elements pointer _Ptr = this->_Getal().allocate(_Count); _TRY_BEGIN _Umove(this->_Myfirst(), this->_Mylast(), _Ptr); _CATCH_ALL this->_Getal().deallocate(_Ptr, _Count); _RERAISE; _CATCH_END size_type _Size = size(); if (this->_Myfirst() != pointer()) { // destroy and deallocate old array _Destroy(this->_Myfirst(), this->_Mylast()); this->_Getal().deallocate(this->_Myfirst(), this->_Myend() - this->_Myfirst()); } this->_Orphan_all(); this->_Myend() = _Ptr + _Count; this->_Mylast() = _Ptr + _Size; this->_Myfirst() = _Ptr; } void _Reserve(size_type _Count) { // ensure room for _Count new elements, grow exponentially if (_Unused_capacity() < _Count) { // need more room, try to get it if (max_size() - size() < _Count) _Xlen(); _Reallocate(_Grow_to(size() + _Count)); } } size_type size() const _NOEXCEPT { // return length of sequence return (this->_Mylast() - this->_Myfirst()); }
Problems
But there are some problems with reserve :
end() will be equal to begin()
23.2.1 General requirements for containers
5:
end() returns an iterator, which is the final value for the container.
iterator end() _NOEXCEPT { // return iterator for end of mutable sequence return (iterator(this->_Mylast(), &this->_Get_data())); }
i.e. _Mylast() will be equal to _Myfirst()
- at () throws an out_of_range exception.
23.2.3 Sequence Containers
17:
The member function in () provides limited access to the elements of the container. at () returns out_of_range if n> = a.size ().
- in the VisualStudio debugger we can see vector values ββwhen the size is not 0
with resize :

with reserve and manually set #define _ITERATOR_DEBUG_LEVEL 0 :
