迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ STL 中使用 STL 列表容器

作者:迹忆客 最近更新:2023/03/23 浏览次数:

本文将演示如何在 C++ 中使用 STL list 容器的多种方法。

在 C++ 中使用 std::list<T> 声明列表容器对象

std::list 容器是标准模板库的一部分,它实现了一个列表数据结构,该结构提供恒定时间从任何位置插入/删除元素。它通常被实现为一个双向链表并支持双向迭代而不是 std::forward_list。不利的一面是,std::list 不具备快速随机访问诸如 std::vectorstd::deque 之类的元素的能力。它只提供两个常量时间函数,frontback 来访问第一个和最后一个元素。std::list 可以使用给定的数据类型和通用初始化列表符号进行初始化,如以下示例代码所示。push_backpush_front 方法可用于将元素添加到列表的任一侧。

#include <algorithm>
#include <iostream>
#include <list>

using std::cout; using std::endl;
using std::list;

template<typename T>
void printList(list<T> l) {
    for (const auto &item : l) {
        cout << item << "; ";
    }
    cout << endl;
}

int main()
{
    std::list<int> l1 = { 11, 12, 13, 14 };

    l1.push_front(15);
    printList(l1);
    l1.push_back(16);
    printList(l1);

    return EXIT_SUCCESS;
}

输出:

15; 11; 12; 13; 14;
15; 11; 12; 13; 14; 16;

在 C++ 中使用 insert() 函数在列表中的指定位置插入元素

insert() 成员函数可用于在给定位置添加元素。该函数有多个重载,第一个重载只有两个参数:迭代器和对对象的引用。给定元素插入到迭代器指向的元素之前。下一个代码片段展示了如何在列表中查找特定值,然后在它之前插入所需的元素。

#include <algorithm>
#include <iostream>
#include <list>

using std::cout; using std::endl;
using std::list;

template<typename T>
void printList(list<T> l) {
    for (const auto &item : l) {
        cout << item << "; ";
    }
    cout << endl;
}

int main()
{
    std::list<int> l1 = { 11, 12, 13, 14 };

    printList(l1);
    auto iter = std::find(l1.begin(), l1.end(), 13);
    if (iter != l1.end()) {
        l1.insert(iter, 55);
    }
    printList(l1);


    return EXIT_SUCCESS;
}

输出:

11; 12; 13; 14;
11; 12; 55; 13; 14;

在 C++ 中使用 swap() 函数交换两个列表的元素

std::list 容器的另一个有用的成员函数是 swap(),它将列表对象的元素与作为唯一参数传递的另一个列表交换。请注意,此操作不会移动或复制单个元素,并且所有迭代器/引用在函数调用后仍然有效。

#include <algorithm>
#include <iostream>
#include <list>

using std::cout; using std::endl;
using std::list;

template<typename T>
void printList(list<T> l) {
    for (const auto &item : l) {
        cout << item << "; ";
    }
    cout << endl;
}

int main()
{
    std::list<int> l1 = { 11, 12, 13, 14 };
    std::list<int> l2 = { 1, 2, 3, 4, 11 };

    cout << "l2: ";
    printList(l2);
    l2.swap(l1);
    cout << "l2: ";
    printList(l2);

    return EXIT_SUCCESS;
}

输出:

l2: 1; 2; 3; 4; 11;
l2: 11; 12; 13; 14;

使用 merge() 函数将两个排序列表合并为一个

或者,可以使用 merge 成员函数将两个排序列表的元素合并为一个。请注意,两个列表都应按升序排序。merge 引用列表对象,其中的元素合并到调用者对象中。操作后,作为参数传递的列表对象变为空。该函数按升序对结果列表对象的元素进行排序,如以下代码示例所示。

#include <algorithm>
#include <iostream>
#include <list>

using std::cout; using std::endl;
using std::list;

template<typename T>
void printList(list<T> l) {
    for (const auto &item : l) {
        cout << item << "; ";
    }
    cout << endl;
}

int main()
{
    std::list<int> l1 = { 8, 10, 2, 4 };
    std::list<int> l2 = { 7, 3, 9, 5, 1 };

    l2.sort();
    l1.sort();
    cout << "l2: ";
    printList(l2);

    l2.merge(l1);

    cout << "l2: ";
    printList(l2);

    return EXIT_SUCCESS;
}

输出:

l2: 1; 3; 5; 7; 9;
l2: 1; 2; 3; 4; 5; 7; 8; 9; 10;

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便