首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::list::resize

void resize( size_type count, T value = T() );

?

(until C++11)

void resize( size_type count );

(1)

(since C++11)

void resize( size_type count, const value_type& value );

(2)

(since C++11)

调整容器的大小以包含count元素。

如果当前大小大于count,容器被简化为第一个容器。count元素。

If the current size is less than count, additional elements are appended and initialized with copies of value.

(until C++11)

If the current size is less than count, 1) additional default-inserted elements are appended 2) additional copies of value are appended

(since C++11)

参数

count

-

new size of the container

value

-

the value to initialize the new elements with

类型要求

T必须符合DefaultInsertable的要求,才能使用过载%281%29。

T必须满足CopyInsertable的要求才能使用过载%282%29。

返回值

%280%29

复杂性

当前大小与count...

二次

代码语言:javascript
复制
#include <iostream>
#include <list>
int main()
{
    std::list<int> c = {1, 2, 3};
    std::cout << "The list holds: ";
    for(auto& el: c) std::cout << el << ' ';
    std::cout << '\n';
    c.resize(5);
    std::cout << "After resize up 5: ";
    for(auto& el: c) std::cout << el << ' ';
    std::cout << '\n';
    c.resize(2);
    std::cout << "After resize down to 2: ";
    for(auto& el: c) std::cout << el << ' ';
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
The list holds: 1 2 3
After resize up 5: 1 2 3 0 0
After resize down to 2: 1 2

二次

另见

size

returns the number of elements (public member function)

insert

inserts elements (public member function)

erase

erases elements (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com