在线文档教程
C++
容器 | Containers

std::list::resize

STD::列表::调整大小

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...

二次

#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'; }

二次

产出:

二次

The list holds: 1 2 3 After resize up 5: 1 2 3 0 0 After resize down to 2: 1 2

二次

另见

sizereturns the number of elements (public member function)
insertinserts elements (public member function)
eraseerases elements (public member function)

© cppreference.com

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

http://en.cppreference.com/w/cpp/容器/list/resize