📌  相关文章
📜  C++ STL中的forward_list resize()函数

📅  最后修改于: 2021-05-30 02:05:25             🧑  作者: Mango

forward_list :: resize()是C++ STL中的内置函数,可更改forward_list的大小。如果给定的大小大于当前大小,则将新元素插入到forward_list的末尾。如果给定的大小小于当前大小,则多余的元素将被销毁。

句法:

forwardlist_name.resize(n)

参数:该函数仅接受一个强制性参数n,该参数指定了转发列表的新大小。

返回值:该函数不返回任何内容。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// forward_list::resize() function
#include 
using namespace std;
  
int main()
{
    forward_list fl = { 10, 20, 30, 40, 50 };
  
    // Prints the forward list elements
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    cout << endl;
  
    // resize to 7
    fl.resize(7);
  
    // // Prints the forward list elements after resize()
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    return 0;
}
输出:
The contents of forward list :10 20 30 40 50 
The contents of forward list :10 20 30 40 50 0 0

程式2:

// C++ program to illustrate the
// forward_list::resize() function
#include 
using namespace std;
  
int main()
{
    forward_list fl = { 10, 20, 30, 40, 50 };
  
    // Prints the forward list elements
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    cout << endl;
  
    // resize to 3
    fl.resize(3);
  
    // Prints the forward list elements after resize()
    cout << "The contents of forward list :";
    for (auto it = fl.begin(); it != fl.end(); ++it)
        cout << *it << " ";
  
    return 0;
}
输出:
The contents of forward list :10 20 30 40 50 
The contents of forward list :10 20 30
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”