std函数 reserve/resize

reserve函数:

void reserve (size_type n);

vector 对象有 capacity属性和allocator属性,其中capacity属性描述“预分配”内存大小;reverse()函数可以设置该值,但是不分配其内存。

resize函数:

void resize (size_type n);
void resize (size_type n, const value_type& val);

resize()函数为vector分配内存,并可初始化;

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.(通过增加/删除元素完成)。

猜你喜欢

转载自blog.csdn.net/mistakk/article/details/81078030
今日推荐