Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีสร้าง List ด้วย Constructor ใน C++ STL


ในบทช่วยสอนนี้ เราจะอภิบายโปรแกรมเพื่อทำความเข้าใจวิธีสร้าง List ด้วย constructor ใน C++ STL

List เป็นโครงสร้างข้อมูลสำหรับเก็บองค์ประกอบในหน่วยความจำแบบไม่ต่อเนื่อง พวกเขาแทรกและลบได้เร็วกว่า vectors

ตัวอย่าง

#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