ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อทำความเข้าใจวิธีสร้างรายการด้วยตัวสร้างใน C++ STL
รายการเป็นโครงสร้างข้อมูลเพื่อจัดเก็บองค์ประกอบในหน่วยความจำในลักษณะที่ไม่ต่อเนื่องกัน เป็นการแทรกและลบอย่างรวดเร็วเมื่อเทียบกับเวกเตอร์
ตัวอย่าง
#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