C++ STL容器:序列式容器-链list,forward_list

摘要:

  CC++ STL(Standard Template Library,标准模板库)在C++编程中的重要性不容忽视,STL提供了一系列容器、迭代器、算法和函数对象,这些组件极大地提高了C++程序的开发效率和代码质量。

STL 容器 分为 2 大类 , 分别是“序列式容器” 和“关联式容器 ”。

  • 序列式容器:每个元素都有固定位置,取决于插入时机和地点,其底层为线性序列的数据结构,里面存储的是元素本身。
  • 关联式容器:元素位置取决于特定的排序准则,和插入顺序无关,其里面存储的是< key , value >结构的键值对,在数据检索时比序列式容器效率更高。

  本系列博文将详细介绍C++STL的各种容器的特性优缺点,以及其常用算法方法等。本文介绍的是序列式容器-链list,forward_list。

(开发环境:VScode,C++17)

关键词C++STL数据存储数据类型链表listforward_list

声明:本文作者原创,转载请附上文章出处与本文链接。

正文:

list

在C++标准模板库(STL)中,list 是一个双向链表容器,它允许在常数时间内从链表的任何位置插入和删除元素。与 vectordeque 相比,list 在内存中的元素不是连续存储的,这意味着它不支持对元素的直接访问(即没有 operator[]),但它提供了在链表中的任何位置进行快速插入和删除的能力。(有需要更深入了解数据结构链的,可看同专栏下数据结构分支)。

常用函数:
  • size():返回链表中元素的数量。
  • empty():检测容器是否为空。
  • begin():返回指向链表第一个元素的迭代器。
  • end():返回指向链表尾后位置的迭代器。
  • push_back():在链表末尾插入一个元素。
  • push_front():在链表开头插入一个元素。
  • pop_back():删除链表末尾一个元素。
  • pop_front():删除链表开头一个元素。
  • insert():在迭代器 pos 指向的位置之前插入元素,并返回指向新插入元素的迭代器。
  • erase():删除迭代器 pos 指向的元素,并返回指向下一个元素的迭代器。
使用例子:
#include <iostream>
#include <list>

int main()
{
    // 创建一个空的 std::list
    std::list<int> myList;

    // 使用 empty 检查列表是否为空
    std::cout << "Is the list empty? " << (myList.empty() ? "Yes" : "No") << std::endl;
  
    // 使用 push_back 在列表末尾添加元素
    myList.push_back(1);
    myList.push_back(2);
    myList.push_back(3);
  
    // 使用 size 获取列表中的元素数量
    std::cout << "Size of the list: " << myList.size() << std::endl;
  
    // 使用 begin 和 end 获取迭代器
    for (std::list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
  
    // 使用 push_front 在列表开头添加元素
    myList.push_front(0);
  
    // 使用 insert 在特定位置插入元素
    // 注意:insert 需要一个迭代器作为插入位置,以及要插入的值
    std::list<int>::iterator it_to_insert = myList.begin(); 
    std::advance(it_to_insert, 2); // 假设我们想在第三个位置插入元素
    myList.insert(it_to_insert, 100);

    // 再次打印列表以查看插入效果
    for (std::list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 使用 erase 删除元素
    // 可以通过迭代器删除单个元素,或者通过两个迭代器删除一个范围
    it_to_insert = myList.begin();
    std::advance(it_to_insert, 3); // 假设我们想删除第四个元素
    myList.erase(it_to_insert);
  
    // 打印列表以查看删除效果
    for (std::list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}
forward_list

forward_list 是 C++ 标准模板库 (STL) 中的一个容器,它表示一个单向链表。与 list 不同,forward_list 是一种更简单的链表实现,因为它只包含指向前一个元素的链接(对于头元素之外的所有元素),而没有指向后一个元素的链接。

常用函数:
  • size():返回链表中元素的数量。
  • empty():检测容器是否为空。
  • begin():返回指向链表第一个元素的迭代器。
  • end():返回指向链表尾后位置的迭代器。
  • push_front():在链表开头插入一个元素。
  • pop_front():删除链表头部的一个元素。
  • insert_after():在指定位置之后插入一个新元素,并返回一个指向新元素的迭代器。
  • erase_after():删除容器中某个指定位置或区域内的所有元素。
使用例子:
#include <iostream>  
#include <forward_list>  
  
int main()
{
    // 创建一个空的 forward_list
    std::forward_list<int> myList;

    // 使用 push_front 在列表开头添加元素
    myList.push_front(1);
    myList.push_front(2);
    myList.push_front(3);
  
    // 使用 size 获取列表中的元素数量
    std::cout << "Size of the forward_list: " << myList.size() << std::endl;
  
    // 使用 empty 检查列表是否为空
    std::cout << "Is the forward_list empty? " << (myList.empty() ? "Yes" : "No") << std::endl;
  
    // 使用 begin 和 end 获取迭代器
    for (std::forward_list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
  
    // 使用 insert_after 在特定位置之后插入元素
    // 注意:forward_list 没有 insert 函数,而是 insert_after
    auto it_to_insert_after = myList.begin();
    std::advance(it_to_insert_after, 1); // 假设我们想在第二个元素之后插入元素
    myList.insert_after(it_to_insert_after, 100);

    // 再次打印列表以查看插入效果
    for (std::forward_list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 使用 erase_after 删除元素
    // 需要先找到要删除的元素的迭代器
    auto it_to_erase = myList.begin();
    std::advance(it_to_erase, 2); // 假设我们想删除第三个元素
    myList.erase_after(it_to_erase);
  
    // 打印列表以查看删除效果
    for (std::forward_list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

推荐阅读

博客主页:https://blog.csdn.net/weixin_45068267
(客官逛一逛,有许多其它有趣的专栏博文)

C/C++专栏:https://blog.csdn.net/weixin_45068267/category_12268204.html
(内含其它STL容器使用及对应的数据结构实现)

  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值