[C++] 基础教程 - std::forward_list库介绍和使用场景

1. std::forward_list库介绍

std::forward_list 是支持从容器中的任何位置快速插入和移除元素的容器。不支持快速随机访问。它实现为单链表,且实质上与其在 C 中的实现相比无任何开销。与 std::list 相比,此容器在不需要双向迭代时提供更好的存储空间效率

在链表内或跨数个链表添加、移除和移动元素,不会使当前指代链表中其他元素的迭代器失效。然而,在从链表移除元素(通过 erase_after)时,指代对应元素的迭代器或引用会失效。

由于单链表没有双向链表那样灵活,因此相比 list 容器,forward_list 容器的功能受到了很多限制。比如,由于单链表只能从前向后遍历,而不支持反向遍历,因此 forward_list 容器只提供前向迭代器,而不是双向迭代器。

forward_list 容器底层使用单链表,也不是一无是处。比如,存储相同个数的同类型元素,单链表耗用的内存空间更少,空间利用率更高,并且对于实现某些操作单链表的执行效率也更高。

效率高是选用 forward_list 而弃用 list 容器最主要的原因,只要是 list 容器和 forward_list 容器都能实现的操作,应优先选择 forward_list 容器。

std::forward_list - cppreference.com

2. 主要的类

std::forward_list主要包含以下几个类:

  • std::forward_list<T>:表示一个存储类型为T的元素的单链表。
  • std::forward_list<T>::iterator:表示指向std::forward_list中元素的迭代器。
  • std::forward_list<T>::const_iterator:表示指向std::forward_list中元素的常量迭代器。

3. 主要的方法

std::forward_list提供了许多方法来操作其元素,以下是一些常用的方法:

  • push_front():在std::forward_list的开头添加一个新元素。
  • erase():删除指定位置的元素或一段范围内的元素。
  • clear():删除std::forward_list中的所有元素。
  • resize():调整std::forward_list的大小。
  • assign():将另一个std::forward_list的内容复制到当前列表。
  • merge():将另一个std::forward_list的内容合并到当前列表。
  • reverse():反转std::forward_list中元素的顺序。
  • sort():对std::forward_list中的元素进行排序。

4. 使用案例

以下是一些使用std::forward_list的示例:

创建一个std::forward_list并向其中添加元素

#include <iostream>
#include <forward_list>

int main() {
    std::forward_list<int> myList;
    myList.push_front(1);
    myList.push_front(2);

    for (auto it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出结果为:2 1

从std::forward_list中删除元素

#include <iostream>
#include <forward_list>

int main() {
    std::forward_list<int> myList;
    myList.push_front(1);
    myList.push_front(2);
    myList.push_front(3);
    myList.push_front(4);
    myList.push_front(5);

    myList.remove_if([](const int& i) { return i % 2 == 0; });

    for (auto it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出结果为:5 3 1

将两个std::forward_list合并成一个

#include <iostream>
#include <forward_list>
#include <algorithm>

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

    std::forward_list<int> mergedList;
    mergedList.merge(list1);
    mergedList.merge(list2);

    for (auto it = mergedList.begin(); it != mergedList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出结果为:1 2 3 4 5 6

对std::forward_list进行排序

#include <iostream>
#include <forward_list>
#include <algorithm>

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

    std::forward_list<int> sortedList;
    sortedList.sort();

    for (auto it = sortedList.begin(); it != sortedList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出结果为:1 2 3 4 5

5. std::forward_list的总结

std::forward_list 是支持从容器中的任何位置快速插入和移除元素的容器。不支持快速随机访问。它实现为单链表,且实质上与其在 C 中的实现相比无任何开销。与 std::list 相比,此容器在不需要双向迭代时提供更好的存储空间效率

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引提到了标准中的std::remove函数,该函数用于删除文件。引用提到了同名的容器成员函数list::remove、list::remove_if、forward_list::remove以及forward_list::remove_if,这些函数用于从容器中擦除被移除的元素。引用提到了在C++的vector容器中,实际删除元素使用的是容器中的std::vector::erase()方法。 综上所述,std::remove在不同上下文中有不同的含义和用法。在标准中,它用于删除文件;在容器中,它用于擦除容器中的元素;在C++的vector容器中,使用std::vector::erase()方法来实际删除元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [c++标准模板(STL)- 算法 (std::remove, std::remove_if)](https://blog.csdn.net/qq_40788199/article/details/127511175)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [std::remove](https://blog.csdn.net/brucethl/article/details/82900667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老狼IT工作室

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值