c++ 11 链表容器新增加了std::forward_list, 它与std::list有什么不同, 学习学习。
std::forward_list - cppreference.com
1. 定义
std::forward_list 是支持从容器中的任何位置快速插入和移除元素的容器。不支持快速随机访问。它实现为单链表,且实质上与其在 C 中的实现相比无任何开销。与 std::list 相比,此容器在不需要双向迭代时提供更好的存储空间效率。
在链表内或跨数个链表添加、移除和移动元素,不会使当前指代链表中其他元素的迭代器失效。然而,在从链表移除元素(通过 erase_after)时,指代对应元素的迭代器或引用会失效。
std::forward_list 满足容器 (Container) (但不包括 size 成员函数,且 operator== 的复杂度始终为线性)、知分配器容器 (AllocatorAwareContainer) 和序列容器 (SequenceContainer) 的要求。
成员类型
成员类型 | 定义 |
value_type | T |
allocator_type | Allocator |
size_type | 无符号整数类型(通常是 std::size_t) |
difference_type | 有符号整数类型(通常是 std::ptrdiff_t) |
reference | value_type& |
const_reference | const value_type& |
pointer | std::allocator_traits |
const_pointer | std::allocator_traits |
iterator | 指向 value_type 的常老式向前迭代器 (LegacyForwardIterator) |
const_iterator | 指向 const value_type 的老式向前迭代器 (LegacyForwardIterator) |
成员函数
(构造函数) | 构造 forward_list (公开成员函数) |
(析构函数) | 析构 forward_list (公开成员函数) |
operator= | 赋值给容器 (公开成员函数) |
assign | 将值赋给容器 (公开成员函数) |
assign_range (C++23) | 将一个范围的值赋给容器 (公开成员函数) |
get_allocator | 返回关联的分配器 (公开成员函数) |
元素访问 | |
front | 访问第一个元素 (公开成员函数) |
迭代器 | |
before_begin cbefore_begin | 返回指向容器开头之前的迭代器 (公开成员函数) |
begin cbegin | 返回指向起始的迭代器 (公开成员函数) |
end cend | 返回指向末尾的迭代器 (公开成员函数) |
容量 | |
empty | 检查容器是否为空 (公开成员函数) |
max_size | 返回可容纳的最大元素数 (公开成员函数) |
修改器 | |
clear | 清除内容 (公开成员函数) |
insert_after | 在某个元素后插入新元素 (公开成员函数) |
emplace_after | 在元素后原位构造元素 (公开成员函数) |
insert_range_after (C++23) | 插入元素范围到元素后 (公开成员函数) |
erase_after | 擦除元素后的元素 (公开成员函数) |
push_front | 插入元素到容器起始 (公开成员函数) |
emplace_front | 在容器头部原位构造元素 (公开成员函数) |
prepend_range (C++23) | 添加元素的范围到起始 (公开成员函数) |
pop_front | 移除首元素 (公开成员函数) |
resize | 改变存储元素的个数 (公开成员函数) |
swap | 交换内容 (公开成员函数) |
操作 | |
merge | 合并两个有序列表 (公开成员函数) |
splice_after | 从另一 forward_list 移动元素 (公开成员函数) |
remove remove_if | 移除满足特定标准的元素 (公开成员函数) |
reverse | 反转元素的顺序 (公开成员函数) |
unique | 删除连续的重复元素 (公开成员函数) |
sort | 对元素进行排序 (公开成员函数) |
2. 示例
- 初始化
#include
#include
int main() {
auto show = [](const char* str, const std::forward_list& f) {
std::cout < f1{1, 2, 3, 4, 5};
show("f1", f1);
std::forward_list f2(f1);
show("f2", f2);
std::forward_list f3 = f1;
show("f3", f3);
std::forward_list f4;
f4.assign(f1.begin(), f1.end());
show("f4", f4);
std::forward_list f5;
f5.assign({ 1, 2, 3, 4, 5 });
show("f5", f5);
std::forward_list f6;
f6.assign(5, 0);
show("f6", f6);
return 0;
}
- 迭代器
#include
#include
#include
int main() {
std::forward_list f1{1, 2, 3, 4, 5};
{
auto iter = f1.begin();
iter++;
//iter--; 错误
//如名字所示, 只能向进,不能后退
}
//注意: 返回指向容器开头之前的迭代器
//就是出现一个空的项, 在首项之前.
for (auto iter = f1.before_begin(); iter != f1.end(); ++iter) {
if (iter == f1.before_begin()) {
//应处理一些初始化动作
std::cout << "before_begin : ";
continue;
}
std::cout << *iter << " ";
}
std::cout << std::endl;
//打印输出 : 0 1 2 3 4 5
//std::forward_list::iterator
for (auto iter = f1.begin(); iter != f1.end(); ++iter) {
std::cout << *iter << " ";
}
std::cout << std::endl;
//std::forward_list::const_iterator iter
for (auto iter = f1.cbegin(); iter != f1.cend(); ++iter) {
std::cout << *iter << " ";
}
std::cout << std::endl;
for (auto elem : f1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::for_each(f1.begin(), f1.end(), [](int& elem) {
std::cout << elem << " ";
});
std::cout << std::endl;
return 0;
}
/*
before_begin : 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
*/
- 其它
#include
#include
#include
#include
int main() {
auto show = [](const char* str, const std::forward_list& f) {
std::cout << str << " : ";
for (const auto& v : f) {
std::cout << v << " ";
}
std::cout << std::endl;
};
std::forward_list lst;
std::forward_list::iterator iter;
//添加元素
//iter = lst.insert_after(lst.begin(), 2); //错误
//iter = lst.emplace_after(lst.end(), 2); //错误
//lst.begin() == lst.end(), 不能在空项插入.
iter = lst.insert_after(lst.before_begin(), 2);
iter = lst.emplace_after(iter, 3);
lst.emplace_front(1);
lst.push_front(0);
show("add ", lst);
//删除元素
lst.pop_front();
show("pop_front", lst);
lst.erase_after(lst.begin());
show("erase", lst);
lst.remove(3);
show("remove=3", lst);
lst.remove_if([](int& x) {return x == 1; });
show("remove_if=1", lst);
lst.assign({1, 2, 3, 4, 5});
lst.resize(10, 9);
show("resize", lst);
lst.unique();
show("unique", lst);
lst.reverse();
show("reverse", lst);
lst.sort();
show("sort", lst);
return 0;
}
/*打印输出
add : 0 1 2 3
pop_front : 1 2 3
erase : 1 3
remove=3 : 1
remove_if=1 :
resize : 1 2 3 4 5 9 9 9 9 9
unique : 1 2 3 4 5 9
reverse : 9 5 4 3 2 1
sort : 1 2 3 4 5 9
*/
3. 总结
通过上面的了解, std::forward_list只是对单链表进行一个简单的封装, 追求的是空间效率、性能。 非必要还是使用std::list吧。