如何在C++ STL中使用建構函式建立List


在本教程中,我們將討論一個程式,瞭解如何使用C++ STL中的建構函式建立List。

List是用於以非連續方式在記憶體中儲存元素的資料結構。與向量相比,插入和刪除是十分快速的。

範例

 線上演示

#include <iostream>
#include <list>
using namespace std;
//printing the list
void print_list(list<int> mylist){
   list<int>::iterator it;
   //printing all the elements
   for (it = mylist.begin(); it != mylist.end(); ++it)
      cout << ' ' << *it;
   cout << '\n';
}
int main(){
   //creating list with help of constructor
   list<int> myList(10, 100);
   print_list(myList);
   return 0;
}

輸出

100 100 100 100 100 100 100 100 100 100

更新時間:25-二月-2020

215次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始行動
廣告
© . All rights reserved.