C++ Forward_list 库 - resize() 函数
描述
C++ 函数 std::forward_list::resize() 更改 forward_list 的大小。如果 n 小于当前大小,则销毁额外的元素。如果 n 大于当前容器大小,则在 forward_list 的末尾插入新元素。
声明
以下是 std::forward_list::resize() 函数形式 std::forward_list 头的声明。
C++11
void resize (size_type n);
参数
n − 要插入的元素数。
返回值
None
异常
如果重新分配失败,则抛出 bad_alloc 异常。
时间复杂度
线性,即 O(n)
示例
下面的例子展示了 std::forward_list::resize() 函数的用法。
#include <iostream>
#include <forward_list>
using namespace std;
bool foo(int n) {
return (n > 5);
}
int main(void) {
forward_list<int> fl;
fl.resize(5);
cout << "List contents after resize operation" << endl;
for (auto it = fl.begin(); it != fl.end(); ++it)
cout << *it << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
List contents after resize operation 0 0 0 0 0

