C++,STL 036(24.10.20)

内容

list容器(链表)的赋值操作。

运行代码

cpp 复制代码
#include <iostream>
#include <list>

using namespace std;

void printList(const list<int> &l)
{
    for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    list<int> l1;
    l1.push_back(1);
    l1.push_back(2);
    l1.push_back(3);
    l1.push_back(4);

    list<int> l2;
    l2 = l1; // 用重载等号赋值
    printList(l2);

    list<int> l3;
    l3.assign(l2.begin(), l2.end()); // 用assign函数赋值[beg, end)里的元素
    printList(l3);

    list<int> l4;
    l4.assign(10, 100); // 用assign函数批量赋值元素
    printList(l4);
}

int main()
{
    test01();

    return 0;
}

输出结果

相关推荐
xlp666hub1 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网2 小时前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub5 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
不想写代码的星星5 小时前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub21 小时前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
不想写代码的星星1 天前
C++继承、组合、聚合:选错了是屎山,选对了是神器
c++
不想写代码的星星2 天前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
齐生13 天前
iOS 知识点 - 渲染机制、动画、卡顿小集合
笔记
用户962377954483 天前
VulnHub DC-1 靶机渗透测试笔记
笔记·测试
樱木Plus4 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++