【C++】std::forward_list

std::forward_list:

std::forward_list 是 C++ 标准库中的一个单向链表容器。与 std::list 相比,std::forward_list 只允许单向遍历,即只能从前往后访问元素。它的实现比 std::list 更轻量,因此在内存和性能方面通常更优,但缺少双向访问的能力。

特性

  1. 单向链表 : std::forward_list 只包含指向下一个元素的指针,不包含指向前一个元素的指针。
  2. 非随机访问 : 与 std::list 相同,不支持通过下标访问元素。
  3. 动态大小: 容器大小可以动态调整。
  4. 稳定的迭代器: 除了被删除的迭代器,其余迭代器在插入和删除操作后保持有效。

常用操作

1. 创建和初始化
cpp 复制代码
#include <forward_list>
#include <iostream>

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

    // 创建一个初始化为 5 个元素的 forward_list,元素值为 10
    std::forward_list<int> fwd_lst2(5, 10);

    // 使用初始化列表创建 forward_list
    std::forward_list<int> fwd_lst3 = {1, 2, 3, 4, 5};

    return 0;
}
2. 访问和修改元素
cpp 复制代码
#include <forward_list>
#include <iostream>

int main() {
    std::forward_list<int> fwd_lst = {1, 2, 3, 4, 5};

    // 使用迭代器遍历 forward_list
    for (auto it = fwd_lst.begin(); it != fwd_lst.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 修改元素
    auto it = fwd_lst.begin();
    ++it; // 移动到第二个元素
    *it = 20;

    std::cout << "Modified list: ";
    for (const auto& elem : fwd_lst) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

3. 添加和删除元素:

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

int main() {
    std::forward_list<int> fwd_lst = {1, 2, 3, 4, 5};

    // 在前面添加元素
    fwd_lst.push_front(0);

    // 删除前面元素
    fwd_lst.pop_front();

    std::cout << "List after push and pop operations: ";
    for (const auto& elem : fwd_lst) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

4. 插入和删除指定位置的元素

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

int main() {
    std::forward_list<int> fwd_lst = {1, 2, 3, 4, 5};

    // 插入元素
    auto it = fwd_lst.before_begin(); // 指向第一个元素的前面
    fwd_lst.insert_after(it, 10); // 在第一个元素前插入 10

    // 删除指定位置的元素
    it = fwd_lst.begin();
    fwd_lst.erase_after(it); // 删除第一个元素

    std::cout << "List after insert and erase operations: ";
    for (const auto& elem : fwd_lst) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}
5. 其他常用方法
  • size() : 返回 forward_list 中元素的数量。
  • empty() : 判断 forward_list 是否为空。
  • clear() : 清空 forward_list 中的所有元素。
  • front() : 访问 forward_list 的第一个元素。
  • resize() : 不适用于 forward_list,因为不支持随机访问。
相关推荐
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者3 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet3 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠3 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu3 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu3 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库3 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根3 天前
【CMake】List
windows·microsoft·list
TToolss3 天前
删除文件夹里的网盘图标
windows