【STL】顺序容器之list、forward_list用法总结

本文总结了C++ STL中的顺序容器list和forward_list的基本原理、用法、时间复杂度及注意事项。list是双向链表,forward_list是单向链表,两者在链表操作上速度快但不支持随机访问。文章详细介绍了它们的初始化、元素访问、遍历、添加、删除、替换等操作,并探讨了迭代器失效的情况。
摘要由CSDN通过智能技术生成

       

一、基本原理

        list是双向链表,forward_list是单向链表,在链表的任何位置添加或删除元素都很快,但是作为代价,这两个容器都不支持随机访问,为了访问容器中的某一个元素,我们只能遍历整个容器。

       forward_list是C++新标准增加的类型,其设计目标是达到与最好的手写单向链表数据结构相当的性能,因此forward_list没有size操作,因为保存或计算其大小会比手写链表多出额外的开销。对于其他容器而言(比如list),size保证是一个常量时间的操作。

       

       

       

二、用法

       以下都以容器list、forward_list中的元素为int为例,若要保存其他类型的元素,将int换成其他类型即可。使用list、forward_list应该包含以下头文件:

// 包含头文件
#include <list>
#include <forward_list>
// 使用命名空间std
using namespace std;

       

初始化

list<类型名>    链表名; 双向链表
list<int>   li1; 创建一个名为li1的双向链表,li1中的元素类型为int
list<int>   li1(100,7); li1中的元素为100个7
list<int>   li1{1,2,3,……}; 元素为1,2,3,……的li1
list<int>   li2(li1); 链表li2中包含和链表li1一样的元素
list<int>   li2=li1; 链表li2中包含和链表li1一样的元素
forward_list<类型名>    链表名; 单向链表
forward_list<int>   f1; 创建一个名为f1的单向链表,f1中的元素类型为int
forward_list<int>   f1(100,7); f1中的元素为100个7
forward_list<int>   f1{1,2,3,……}; 元素为1,2,3,……的f1
forward_list<int>   f2(f1); 链表f2中包含和链表f1一样的元素
forward_list<int>   f2=f1; 链表f2中包含和链表f1一样的元素

       

访问元素、获取迭代器

list
li1.front(); 访问li1的链首元素
li1.back(); 访问li1的链尾元素
*iter 访问迭代器iter指向的元素
li1.begin();
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`forward_list`是C++ STL的一个单向链表容器。单向链表是一种数据结构,它具有一些优点,如在链表中添加或删除元素更加高效。在使用`forward_list`时,必须使用迭代器来访问元素,而不是像在`vector`或`array`中那样使用下标。以下是`forward_list`的一些基本操作: 1.创建`forward_list`: ```c++ #include <forward_list> #include <iostream> using namespace std; int main() { forward_list<int> f1; // 空的forward_list forward_list<int> f2(3); // 3个元素的forward_list,元素默认值为0 forward_list<int> f3(2, 1); // 2个元素的forward_list,元素默认值为1 forward_list<int> f4{ 1, 2, 3 }; // 3个元素的forward_list,初始化列表为1、2、3 forward_list<int> f5(f4.begin(), f4.end()); // 从迭代器初始化forward_list return 0; } ``` 2.在`forward_list`中插入元素: ```c++ #include <forward_list> #include <iostream> using namespace std; int main() { forward_list<int> f{ 1, 2, 3 }; f.push_front(0); // 在前面插入0 f.insert_after(f.begin(), 4); // 在第一个元素之后插入4 f.insert_after(f.begin(), 2, 5); // 在第一个元素之后插入两个5 f.insert_after(f.begin(), {6, 7, 8}); // 在第一个元素之后插入3个元素 return 0; } ``` 3.在`forward_list`中删除元素: ```c++ #include <forward_list> #include <iostream> using namespace std; int main() { forward_list<int> f{ 1, 2, 3 }; f.pop_front(); // 删除第一个元素 f.erase_after(f.begin()); // 删除第二个元素 f.remove(3); // 删除所有等于3的元素 return 0; } ``` 4.在`forward_list`中查找元素: ```c++ #include <forward_list> #include <iostream> using namespace std; int main() { forward_list<int> f{ 1, 2, 3 }; auto it = find(f.begin(), f.end(), 3); if (it != f.end()) cout << "3 is in the forward_list." << endl; else cout << "3 is not in the forward_list." << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值