c++ 疑难杂症(11) std::forward_list
ztj100 2025-01-14 19:13 23 浏览 0 评论
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<Allocator>::pointer |
const_pointer | std::allocator_traits<Allocator>::const_pointer |
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 <iostream>
#include <forward_list>
int main() {
auto show = [](const char* str, const std::forward_list<int>& f) {
std::cout <<str<< " : ";
for (const auto& v : f) {
std::cout << v << " ";
}
std::cout << std::endl;
};
std::forward_list<int> f1{1, 2, 3, 4, 5};
show("f1", f1);
std::forward_list<int> f2(f1);
show("f2", f2);
std::forward_list<int> f3 = f1;
show("f3", f3);
std::forward_list<int> f4;
f4.assign(f1.begin(), f1.end());
show("f4", f4);
std::forward_list<int> f5;
f5.assign({ 1, 2, 3, 4, 5 });
show("f5", f5);
std::forward_list<int> f6;
f6.assign(5, 0);
show("f6", f6);
return 0;
}
- 迭代器
#include <iostream>
#include <forward_list>
#include <algorithm>
int main() {
std::forward_list<int> 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<int>::iterator
for (auto iter = f1.begin(); iter != f1.end(); ++iter) {
std::cout << *iter << " ";
}
std::cout << std::endl;
//std::forward_list<int>::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 <iostream>
#include <forward_list>
#include <algorithm>
#include <list>
int main() {
auto show = [](const char* str, const std::forward_list<int>& f) {
std::cout << str << " : ";
for (const auto& v : f) {
std::cout << v << " ";
}
std::cout << std::endl;
};
std::forward_list<int> lst;
std::forward_list<int>::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吧。
相关推荐
- 其实TensorFlow真的很水无非就这30篇熬夜练
-
好的!以下是TensorFlow需要掌握的核心内容,用列表形式呈现,简洁清晰(含表情符号,<300字):1.基础概念与环境TensorFlow架构(计算图、会话->EagerE...
- 交叉验证和超参数调整:如何优化你的机器学习模型
-
准确预测Fitbit的睡眠得分在本文的前两部分中,我获取了Fitbit的睡眠数据并对其进行预处理,将这些数据分为训练集、验证集和测试集,除此之外,我还训练了三种不同的机器学习模型并比较了它们的性能。在...
- 机器学习交叉验证全指南:原理、类型与实战技巧
-
机器学习模型常常需要大量数据,但它们如何与实时新数据协同工作也同样关键。交叉验证是一种通过将数据集分成若干部分、在部分数据上训练模型、在其余数据上测试模型的方法,用来检验模型的表现。这有助于发现过拟合...
- 深度学习中的类别激活热图可视化
-
作者:ValentinaAlto编译:ronghuaiyang导读使用Keras实现图像分类中的激活热图的可视化,帮助更有针对性...
- 超强,必会的机器学习评估指标
-
大侠幸会,在下全网同名[算法金]0基础转AI上岸,多个算法赛Top[日更万日,让更多人享受智能乐趣]构建机器学习模型的关键步骤是检查其性能,这是通过使用验证指标来完成的。选择正确的验证指...
- 机器学习入门教程-第六课:监督学习与非监督学习
-
1.回顾与引入上节课我们谈到了机器学习的一些实战技巧,比如如何处理数据、选择模型以及调整参数。今天,我们将更深入地探讨机器学习的两大类:监督学习和非监督学习。2.监督学习监督学习就像是有老师的教学...
- Python 模型部署不用愁!容器化实战,5 分钟搞定环境配置
-
你是不是也遇到过这种糟心事:花了好几天训练出的Python模型,在自己电脑上跑得顺顺当当,一放到服务器就各种报错。要么是Python版本不对,要么是依赖库冲突,折腾半天还是用不了。别再喊“我...
- 神经网络与传统统计方法的简单对比
-
传统的统计方法如...
- 自回归滞后模型进行多变量时间序列预测
-
下图显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。假设要预测其中一个变量。比如,sparklingwine。如何建立一个模型来进行预测呢?一种常见的方...
- 苹果AI策略:慢哲学——科技行业的“长期主义”试金石
-
苹果AI策略的深度原创分析,结合技术伦理、商业逻辑与行业博弈,揭示其“慢哲学”背后的战略智慧:一、反常之举:AI狂潮中的“逆行者”当科技巨头深陷AI军备竞赛,苹果的克制显得格格不入:功能延期:App...
- 时间序列预测全攻略,6大模型代码实操
-
如果你对数据分析感兴趣,希望学习更多的方法论,希望听听经验分享,欢迎移步宝藏公众号...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)