功能描述:

    • 创建list容器

    函数原型:

    • listlst; //list采用模板类实现,对象的默认构造形式
    • list(beg,end); //构造函数将[beg,end)区间中的元素拷贝给本身
    • list(n,elem);//构造函数将n个elem拷贝给本身
    • list(const list &lst); //拷贝构造函数

    代码实现:

    1. #include<iostream>
    2. #include<list>
    3. using namespace std;
    4. //list容器构造函数
    5. void printList(const list<int>& L) {
    6. for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
    7. cout << *it << " ";
    8. }
    9. cout << endl;
    10. }
    11. void test01() {
    12. //创建list容器
    13. list<int>L1;//默认构造
    14. //添加数据
    15. L1.push_back(10);
    16. L1.push_back(20);
    17. L1.push_back(30);
    18. L1.push_back(40);
    19. //遍历容器
    20. printList(L1);
    21. //区间构造
    22. list<int>L2(L1.begin(), L1.end());
    23. printList(L2);
    24. //拷贝构造
    25. list<int>L3(L1);
    26. printList(L3);
    27. //n个elem
    28. list<int>L4(10, 1000);
    29. printList(L4);
    30. }
    31. int main() {
    32. test01();
    33. return 0;
    34. }

    运行结果:
    image.png