第一章 std::forward_list 容器函数列表与讲解
一、构造函数
默认构造
- 函数功能:创建一个空的单向链表。
- 函数入参:无。
- 函数返回值:无(构造对象)。
- 使用举例:
cpp
std::forward_list<int> fl1;
// fl1 为空
填充构造
- 函数功能:创建包含指定数量相同元素的单向链表。
- 函数入参 :
count(元素数量),value(默认值,可选,默认为类型默认值)。 - 函数返回值:无(构造对象)。
- 使用举例:
cpp
std::forward_list<int> fl1(5, 42); // 5个42
std::forward_list<int> fl2(4); // 4个默认值0
范围构造
- 函数功能 :使用迭代器区间
[first, last)内的元素进行构造。 - 函数入参 :
first(起始迭代器),last(结束迭代器)。 - 函数返回值:无(构造对象)。
- 使用举例:
cpp
int arr[] = {10, 20, 30, 40, 50};
std::forward_list<int> fl(std::begin(arr), std::end(arr));
初始化列表构造
- 函数功能 :使用初始化列表
{...}中的元素进行构造。 - 函数入参 :
init(initializer_list)。 - 函数返回值:无(构造对象)。
- 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3, 4, 5};
拷贝构造
- 函数功能:使用另一个 forward_list 的内容进行深拷贝构造。
- 函数入参 :
other(另一个 forward_list 对象)。 - 函数返回值:无(构造对象)。
- 使用举例:
cpp
std::forward_list<int> fl1 = {10, 20, 30};
std::forward_list<int> fl2(fl1);
移动构造
- 函数功能:转移另一个 forward_list 的资源,原对象变为空。
- 函数入参 :
other(右值引用)。 - 函数返回值:无(构造对象)。
- 使用举例:
cpp
std::forward_list<int> fl1 = {100, 200, 300};
std::forward_list<int> fl2(std::move(fl1));
二、赋值操作
operator=
- 函数功能:替换容器内容(支持拷贝、移动、初始化列表)。
- 函数入参:另一个 forward_list 对象、右值引用或初始化列表。
- 函数返回值 :
*this(当前对象的引用)。 - 使用举例:
cpp
std::forward_list<int> fl1 = {1, 2, 3};
std::forward_list<int> fl2;
fl2 = fl1; // 拷贝赋值
fl2 = std::move(fl1); // 移动赋值
fl2 = {100, 200}; // 初始化列表赋值
assign (填充)
- 函数功能:替换容器内容为指定数量的相同值。
- 函数入参 :
count(数量),value(值)。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl;
fl.assign(3, 99);
assign (范围)
- 函数功能 :替换容器内容为迭代器区间
[first, last)的元素。 - 函数入参 :
first,last(迭代器)。 - 函数返回值 :
void。 - 使用举例:
cpp
int arr[] = {11, 22, 33, 44};
std::forward_list<int> fl;
fl.assign(std::begin(arr), std::end(arr));
assign (列表)
- 函数功能:替换容器内容为初始化列表中的元素。
- 函数入参 :
init(initializer_list)。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl;
fl.assign({5, 10, 15, 20});
三、迭代器
before_begin / cbefore_begin
- 函数功能:获取指向容器首元素之前位置的迭代器(用于在头部插入或删除)。
- 函数入参:无。
- 函数返回值 :
iterator/const_iterator。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3};
auto it = fl.before_begin();
fl.insert_after(it, 0); // 在头部插入0
begin / end
- 函数功能:获取指向首元素和尾后位置的正向迭代器。
- 函数入参:无。
- 函数返回值 :
iterator。 - 使用举例:
cpp
for (auto it = fl.begin(); it != fl.end(); ++it)
{
std::cout << *it << " ";
}
cbegin / cend
- 函数功能:获取指向首元素和尾后位置的常量正向迭代器(只读)。
- 函数入参:无。
- 函数返回值 :
const_iterator。 - 使用举例:
cpp
for (auto it = fl.cbegin(); it != fl.cend(); ++it)
{
std::cout << *it << " ";
}
四、容量查询
empty
- 函数功能:检查容器是否为空。
- 函数入参:无。
- 函数返回值 :
bool(true 为空)。 - 使用举例:
cpp
std::forward_list<int> fl;
std::cout << fl.empty(); // 输出 1 (true)
max_size
- 函数功能:获取容器支持的最大元素数量。
- 函数入参:无。
- 函数返回值 :
size_type(最大容量)。 - 使用举例:
cpp
std::forward_list<int> fl;
std::cout << fl.max_size();
五、元素访问
front
- 函数功能:访问容器的第一个元素。
- 函数入参:无。
- 函数返回值:指向首元素的引用。
- 使用举例:
cpp
std::forward_list<int> fl = {10, 20, 30};
fl.front() = 99; // 修改首元素
六、修改器
push_front
- 函数功能:在容器开头添加元素。
- 函数入参 :
value(值)。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl = {2, 3};
fl.push_front(1);
emplace_front
- 函数功能:在容器开头原地构造元素。
- 函数入参:构造参数。
- 函数返回值 :
void(C++17起返回引用)。 - 使用举例:
cpp
std::forward_list<Student> fl;
fl.emplace_front("Alice", 95.5);
pop_front
- 函数功能:移除容器开头的元素。
- 函数入参:无。
- 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3};
fl.pop_front();
insert_after
- 函数功能:在指定迭代器位置之后插入元素(支持单元素、填充、范围、列表)。
- 函数入参 :
pos(迭代器), 值/数量/范围/列表。 - 函数返回值:指向最后一个插入元素的迭代器。
- 使用举例:
cpp
std::forward_list<int> fl = {1, 3, 5};
auto it = fl.begin();
fl.insert_after(it, 2); // 在第一个元素后插入2
fl.insert_after(it, 3, 9); // 插入3个9
emplace_after
- 函数功能:在指定迭代器位置之后原地构造元素。
- 函数入参 :
pos(迭代器), 构造参数。 - 函数返回值:指向新元素的迭代器。
- 使用举例:
cpp
std::forward_list<Student> fl;
auto it = fl.before_begin();
it = fl.emplace_after(it, "Charlie", 90.0);
erase_after
- 函数功能:移除指定迭代器位置之后的一个或区间元素。
- 函数入参 :
pos或first, last(迭代器)。 - 函数返回值:指向被擦除元素后一个元素的迭代器。
- 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3, 4, 5};
auto it = fl.begin();
fl.erase_after(it); // 删除第二个元素(2)
swap
- 函数功能:交换两个容器的内容。
- 函数入参 :
other(另一个 forward_list)。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl1 = {1, 2};
std::forward_list<int> fl2 = {10, 20};
fl1.swap(fl2);
resize
- 函数功能:改变容器中存储的元素数量。
- 函数入参 :
count(新大小),value(可选,填充值)。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3};
fl.resize(6, 99); // 扩大到6,填充99
fl.resize(3); // 缩小到3
clear
- 函数功能:清除所有元素。
- 函数入参:无。
- 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3};
fl.clear();
七、操作
merge
- 函数功能:合并两个已排序的 forward_list(源 list 变为空)。
- 函数入参 :
other(另一个已排序的 list), 可选比较函数。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl1 = {1, 3, 5};
std::forward_list<int> fl2 = {2, 4, 6};
fl1.merge(fl2);
splice_after
- 函数功能:将元素从另一个 forward_list 移入当前 list 的指定位置之后。
- 函数入参 :
pos(目标位置),other(源list), 可选的源位置或范围。 - 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl1 = {1, 2};
std::forward_list<int> fl2 = {10, 20, 30};
fl1.splice_after(fl1.before_begin(), fl2);
remove / remove_if
- 函数功能:移除所有等于特定值或满足谓词条件的元素。
- 函数入参 :
value或pred(一元谓词)。 - 函数返回值 :
void(C++20前),size_type(C++20起)。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3, 2, 4};
fl.remove(2);
fl.remove_if([](int x){ return x % 2 == 0; });
reverse
- 函数功能:反转链表中元素的顺序。
- 函数入参:无。
- 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 2, 3};
fl.reverse();
unique
- 函数功能:移除连续重复的元素。
- 函数入参:可选的二元谓词。
- 函数返回值 :
void(C++20前),size_type(C++20起)。 - 使用举例:
cpp
std::forward_list<int> fl = {1, 1, 2, 2, 3};
fl.unique();
sort
- 函数功能:对链表元素进行排序(forward_list 专用成员函数)。
- 函数入参:可选比较函数。
- 函数返回值 :
void。 - 使用举例:
cpp
std::forward_list<int> fl = {3, 1, 2};
fl.sort();
fl.sort(std::greater<int>());
八、非成员函数
比较运算符
- 函数功能 :按字典序比较两个 forward_list (
==,!=,<,>,<=,>=)。 - 函数入参 :
lhs,rhs。 - 函数返回值 :
bool。 - 使用举例:
cpp
std::forward_list<int> fl1 = {1, 2};
std::forward_list<int> fl2 = {1, 2};
bool eq = (fl1 == fl2);
std::swap
- 函数功能:交换两个 forward_list 容器的内容。
- 函数入参 :
lhs,rhs。 - 函数返回值 :
void。 - 使用举例:
cpp
std::swap(fl1, fl2);
std::erase / std::erase_if (C++20)
- 函数功能:移除所有等于特定值或满足谓词条件的元素。
- 函数入参 :
c(容器),value或pred。 - 函数返回值:被移除的元素数量。
- 使用举例:
cpp
std::erase(fl, 2);
std::erase_if(fl, [](int x){ return x > 5; });
第二章 分模块代码解析
1. 构造函数
展示 forward_list 的多种构造方式,包括默认、填充、范围、初始化列表、拷贝、移动及自定义类型构造。
cpp
void demo_default_constructor()
{
std::forward_list<int> fl1;
print_forward_list(fl1, "[默认构造] 空");
}
void demo_fill_constructor_value()
{
std::forward_list<int> fl(5, 42);
print_forward_list(fl, "[填充构造] 5个42");
}
void demo_fill_constructor_default()
{
std::forward_list<int> fl(4);
print_forward_list(fl, "[填充构造] 4个默认值0");
}
void demo_range_constructor()
{
int arr[] = { 10, 20, 30, 40, 50 };
std::forward_list<int> fl(std::begin(arr), std::end(arr));
print_forward_list(fl, "[范围构造] 数组");
}
void demo_initializer_list_constructor()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
print_forward_list(fl, "[初始化列表构造]");
}
void demo_copy_constructor()
{
std::forward_list<int> fl1 = { 10, 20, 30 };
std::forward_list<int> fl2(fl1);
print_forward_list(fl2, "[拷贝构造] 新列表");
}
void demo_move_constructor()
{
std::forward_list<int> fl1 = { 100, 200, 300 };
std::forward_list<int> fl2(std::move(fl1));
print_forward_list(fl2, "[移动构造]");
}
void demo_custom_type_constructor()
{
std::forward_list<Student> fl = {
Student("Alice", 95.5), Student("Bob", 88.0)
};
print_student_list(fl, "[自定义类型构造]");
}
2. 赋值操作
演示使用 = 和 assign 进行内容替换。
cpp
void demo_copy_assignment()
{
std::forward_list<int> fl1 = { 1, 2, 3 };
std::forward_list<int> fl2;
fl2 = fl1;
print_forward_list(fl2, "[拷贝赋值] 新列表");
}
void demo_move_assignment()
{
std::forward_list<int> fl1 = { 10, 20, 30 };
std::forward_list<int> fl2;
fl2 = std::move(fl1);
print_forward_list(fl2, "[移动赋值]");
}
void demo_initializer_list_assignment()
{
std::forward_list<int> fl;
fl = { 100, 200, 300, 400 };
print_forward_list(fl, "[初始化列表赋值]");
}
void demo_assign_fill()
{
std::forward_list<int> fl;
fl.assign(3, 99);
print_forward_list(fl, "[填充赋值] assign填充3个99");
}
void demo_assign_range()
{
std::forward_list<int> fl;
int arr[] = { 11, 22, 33, 44 };
fl.assign(std::begin(arr), std::end(arr));
print_forward_list(fl, "[范围赋值] assign范围赋值");
}
void demo_assign_initializer_list()
{
std::forward_list<int> fl;
fl.assign({ 5, 10, 15, 20 });
print_forward_list(fl, "[初始化列表赋值] assign初始化列表赋值");
}
3. 迭代器
展示 forward_list 特有的 before_begin 以及常规的正向迭代器。
cpp
void demo_before_begin()
{
std::forward_list<int> fl = { 1, 2, 3 };
auto it = fl.before_begin();
fl.insert_after(it, 0);
print_forward_list(fl, "[before_begin] 插入0");
}
void demo_begin_end()
{
std::forward_list<int> fl = { 10, 20, 30 };
std::cout << "[正向迭代] begin到end遍历: ";
for (auto it = fl.begin(); it != fl.end(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
void demo_cbegin_cend()
{
std::forward_list<int> fl = { 10, 20, 30 };
std::cout << "[const正向迭代] cbegin到cend遍历(只读): ";
for (auto it = fl.cbegin(); it != fl.cend(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
void demo_cbefore_begin()
{
std::forward_list<int> fl = { 1, 2, 3 };
auto it = fl.cbefore_begin();
auto next = std::next(it);
std::cout << "[cbefore_begin] 下一个元素: " << *next << std::endl;
}
4. 容量查询与元素访问
包含判空、最大容量查询,以及唯一的元素访问函数 front。
cpp
void demo_empty()
{
std::forward_list<int> fl1;
std::forward_list<int> fl2 = { 1, 2, 3 };
std::cout << "[empty] 空列表 fl1: " << fl1.empty() << std::endl;
std::cout << "[empty] 非空列表 fl2: " << fl2.empty() << std::endl;
}
void demo_max_size()
{
std::forward_list<int> fl;
std::cout << "[max_size] forward_list<int>: " << fl.max_size() << std::endl;
}
void demo_front()
{
std::forward_list<int> fl = { 10, 20, 30 };
std::cout << "[front] 修改前front: " << fl.front() << std::endl;
fl.front() = 99;
print_forward_list(fl, "[front] 修改front后:");
}
5. 修改器 - 头部操作
展示在链表头部进行添加和删除的操作。
cpp
void demo_push_front()
{
std::forward_list<int> fl = { 2, 3, 4 };
fl.push_front(1);
fl.push_front(0);
print_forward_list(fl, "[push_front]");
}
void demo_emplace_front()
{
std::forward_list<Student> fl;
fl.emplace_front("Alice", 95.5);
fl.emplace_front("Bob", 88.0);
print_student_list(fl, "[emplace_front]");
}
void demo_pop_front()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.pop_front();
fl.pop_front();
print_forward_list(fl, "[pop_front] 两次后");
}
6. 修改器 - insert_after 与 emplace_after
展示在指定位置之后插入或原地构造元素。
cpp
void demo_insert_after_single()
{
std::forward_list<int> fl = { 1, 3, 5 };
auto it = fl.begin();
fl.insert_after(it, 2);
print_forward_list(fl, "[insert_after单元素] 在第一个元素后插入2");
}
void demo_insert_after_fill()
{
std::forward_list<int> fl = { 1, 5 };
auto it = fl.begin();
fl.insert_after(it, 3, 3);
print_forward_list(fl, "[insert_after填充] 在第一个元素后插入3个3");
}
void demo_insert_after_before_begin()
{
std::forward_list<int> fl = { 3, 4, 5 };
auto it = fl.before_begin();
fl.insert_after(it, 1);
fl.insert_after(it, 2);
print_forward_list(fl, "[insert_after] before_begin后连续插入");
}
void demo_emplace_after()
{
std::forward_list<Student> fl;
auto it = fl.before_begin();
it = fl.emplace_after(it, "Charlie", 90.0);
it = fl.emplace_after(it, "Bob", 88.0);
fl.emplace_after(fl.before_begin(), "Alice", 95.5);
print_student_list(fl, "[emplace_after] 自定义类型");
}
7. 修改器 - erase_after
展示删除指定位置之后的元素。
cpp
void demo_erase_after_single()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
auto it = fl.begin();
fl.erase_after(it);
print_forward_list(fl, "[erase_after单元素] 删除第一个元素之后");
}
void demo_erase_after_range()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5, 6 };
auto first = fl.begin();
auto last = std::next(first, 4);
fl.erase_after(first, last);
print_forward_list(fl, "[erase_after范围] 范围删除");
}
void demo_erase_after_head()
{
std::forward_list<int> fl = { 10, 20, 30 };
fl.erase_after(fl.before_begin());
print_forward_list(fl, "[erase_after头部] 删除头部(before_begin之后)");
}
8. 修改器 - swap / resize / clear
包含容器交换、大小调整和清空操作。
cpp
void demo_swap()
{
std::forward_list<int> fl1 = { 1, 2, 3 };
std::forward_list<int> fl2 = { 10, 20, 30, 40 };
fl1.swap(fl2);
print_forward_list(fl1, "[swap] 后fl1");
}
void demo_resize_smaller()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.resize(3);
print_forward_list(fl, "[resize] 缩小到3");
}
void demo_resize_larger_value()
{
std::forward_list<int> fl = { 1, 2, 3 };
fl.resize(6, 99);
print_forward_list(fl, "[resize] 扩大到6(填充99)");
}
void demo_clear()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.clear();
std::cout << "[clear] 后empty: " << fl.empty() << std::endl;
}
9. 操作 - merge / splice_after
展示链表特有的高效合并和拼接操作。
cpp
void demo_merge()
{
std::forward_list<int> fl1 = { 1, 3, 5, 7 };
std::forward_list<int> fl2 = { 2, 4, 6, 8 };
fl1.merge(fl2);
print_forward_list(fl1, "[merge] 后fl1");
}
void demo_merge_custom_comparator()
{
std::forward_list<int> fl1 = { 7, 5, 3, 1 };
std::forward_list<int> fl2 = { 8, 6, 4, 2 };
fl1.merge(fl2, std::greater<int>());
print_forward_list(fl1, "[merge] 降序后fl1");
}
void demo_splice_after_all()
{
std::forward_list<int> fl1 = { 1, 2 };
std::forward_list<int> fl2 = { 10, 20, 30 };
fl1.splice_after(fl1.before_begin(), fl2);
print_forward_list(fl1, "[splice_after全部] 后fl1");
}
void demo_splice_after_range()
{
std::forward_list<int> fl1 = { 1, 5 };
std::forward_list<int> fl2 = { 10, 20, 30, 40, 50 };
auto pos = fl1.begin();
auto first = fl2.before_begin();
auto last = std::next(first, 3);
fl1.splice_after(pos, fl2, first, last);
print_forward_list(fl1, "[splice_after范围] 后fl1");
}
10. 操作 - remove / reverse / unique / sort
展示链表的元素移除、反转、去重和排序操作。
cpp
void demo_remove()
{
std::forward_list<int> fl = { 1, 2, 3, 2, 4, 2, 5 };
fl.remove(2);
print_forward_list(fl, "[remove] 2后");
}
void demo_remove_if()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
fl.remove_if([](int x) { return x % 2 == 0; });
print_forward_list(fl, "[remove_if] 偶数后");
}
void demo_reverse()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.reverse();
print_forward_list(fl, "[reverse] 后fl");
}
void demo_unique()
{
std::forward_list<int> fl = { 1, 1, 2, 2, 2, 3, 3, 4, 1, 1 };
fl.unique();
print_forward_list(fl, "[unique] 连续重复去重");
}
void demo_sort()
{
std::forward_list<int> fl = {5, 3, 1, 4, 2};
fl.sort();
print_forward_list(fl, "[sort] 升序后");
}
void demo_sort_descending()
{
std::forward_list<int> fl = { 5, 3, 1, 4, 2 };
fl.sort(std::greater<int>());
print_forward_list(fl, "[sort] 降序后");
}
11. 非成员函数与综合应用
包含比较运算符、std::swap、C++20 的 std::erase,以及一个综合的学生成绩管理示例。
cpp
void demo_comparsion()
{
std::forward_list<int> fl1 = { 1, 2, 3 };
std::forward_list<int> fl2 = { 1, 2, 3 };
std::cout << "fl1 == fl2: " << (fl1 == fl2) << std::endl;
}
void demo_std_swap()
{
std::forward_list<int> fl1 = { 1, 2 };
std::forward_list<int> fl2 = { 10, 20 };
std::swap(fl1, fl2);
print_forward_list(fl1, "[std::swap] 后fl1");
}
void demo_comprehensive_usage()
{
std::forward_list<Student> students;
students.emplace_front("Dave", 90.1);
students.emplace_front("Alice", 95.5);
auto it = students.begin();
std::advance(it, 1);
students.emplace_after(it, "Eve", 91.0);
students.remove_if([](const Student& s) { return s.getScore() < 60.0; });
students.sort([](const Student& a, const Student& b) {
return a.getScore() > b.getScore();
});
students.reverse();
print_student_list(students, "综合应用: 反转后(分数从低到高)");
}
第三章 完整代码
cpp
#include<iostream>
#include<forward_list>
#include<algorithm>
#include<string>
#include<functional>
#include<iterator>
#include<cmath>
/*
* 非基础类型定义
*/
class Student
{
public:
Student() = default;
Student(const std::string& name, double score) : name_(name), score_(score) { }
Student(const Student& other) : name_(other.name_), score_(other.score_) { }
Student(Student&& other) noexcept : name_(std::move(other.name_)), score_(other.score_) { }
Student& operator=(const Student& other)
{
if (this != &other)
{
name_ = other.name_;
score_ = other.score_;
}
return *this;
}
Student& operator=(Student&& other) noexcept
{
if (this != &other)
{
name_ = std::move(other.name_);
score_ = other.score_;
}
return *this;
}
bool operator==(const Student& other) const
{
return name_ == other.name_ && score_ == other.score_;
}
bool operator!=(const Student& other) const { return !(*this == other); }
bool operator<(const Student& other) const { return score_ < other.score_; }
bool operator>(const Student& other) const { return score_ > other.score_; }
bool operator<=(const Student& other) const { return score_ <= other.score_; }
bool operator>=(const Student& other) const { return score_ >= other.score_; }
const std::string& getName() const { return name_; }
double getScore() const { return score_; }
friend std::ostream& operator<<(std::ostream& os, const Student& s)
{
os << "Student(" << s.name_ << ", " << s.score_ << ")";
return os;
}
private:
std::string name_;
double score_ = 0.0;
};
/*
* 辅助打印函数
*/
template <typename T>
void print_forward_list(const std::forward_list<T>& fl, const std::string& label)
{
std::cout << label << ": ";
for (const auto& elem : fl)
{
std::cout << elem << " ";
}
std::cout << std::endl;
}
void print_student_list(const std::forward_list<Student>& fl, const std::string& label)
{
std::cout << label << ":\n";
for (const auto& s : fl)
{
std::cout << " " << s << "\n";
}
std::cout << std::endl;
}
// =======================================================
/*
* 1. 构造函数
*/
// 1.1 默认构造
void demo_default_constructor()
{
std::forward_list<int> fl1;
print_forward_list(fl1, "[默认构造] 空");
}
// 1.2 填充构造
void demo_fill_constructor_value()
{
std::forward_list<int> fl(5, 42);
print_forward_list(fl, "[填充构造] 5个42");
}
// 1.3 填充构造 默认
void demo_fill_constructor_default()
{
std::forward_list<int> fl(4);
print_forward_list(fl, "[填充构造] 4个默认值0");
}
// 1.4 范围构造
void demo_range_constructor()
{
int arr[] = { 10, 20, 30, 40, 50 };
std::forward_list<int> fl(std::begin(arr), std::end(arr));
print_forward_list(fl, "[范围构造] 数组");
}
// 1.5 初始化列表构造
void demo_initializer_list_constructor()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
print_forward_list(fl, "[初始化列表构造]");
}
// 1.6 拷贝构造
void demo_copy_constructor()
{
std::forward_list<int> fl1 = { 10, 20, 30 };
std::forward_list<int> fl2(fl1);
print_forward_list(fl1, "[拷贝构造] 原始列表");
print_forward_list(fl2, "[拷贝构造] 新列表");
}
// 1.7 移动构造
void demo_move_constructor()
{
std::forward_list<int> fl1 = { 100, 200, 300 };
std::forward_list<int> fl2(std::move(fl1));
print_forward_list(fl2, "[移动构造]");
std::cout << "移动后fl1是否为空: " << fl1.empty() << std::endl;
}
// 1.8 自定义类型构造
void demo_custom_type_constructor()
{
std::forward_list<Student> fl = {
Student("Alice", 95.5),
Student("Bob", 88.0),
Student("Charlie", 92.3)
};
print_student_list(fl, "[自定义类型构造]");
}
// =======================================================
/*
* 2. 赋值操作
*/
// 2.1 拷贝赋值
void demo_copy_assignment()
{
std::forward_list<int> fl1 = { 1, 2, 3 };
std::forward_list<int>fl2;
fl2 = fl1;
print_forward_list(fl1, "[拷贝赋值] 原始列表");
print_forward_list(fl2, "[拷贝赋值] 新列表");
}
// 2.2 移动赋值
void demo_move_assignment()
{
std::forward_list<int> fl1 = { 10, 20, 30 };
std::forward_list<int> fl2;
fl2 = std::move(fl1);
print_forward_list(fl2, "[移动赋值]");
std::cout << "移动后fl1是否为空: " << fl1.empty() << std::endl;
}
// 2.3 初始化列表赋值
void demo_initializer_list_assignment()
{
std::forward_list<int> fl;
fl = { 100, 200, 300, 400 };
print_forward_list(fl, "[初始化列表赋值]");
}
// 2.4 填充赋值
void demo_assign_fill()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.assign(3, 99);
print_forward_list(fl, "[填充赋值] assign填充3个99");
}
// 2.5 范围赋值
void demo_assign_range()
{
std::forward_list<int> fl;
int arr[] = { 11, 22, 33, 44 };
fl.assign(std::begin(arr), std::end(arr));
print_forward_list(fl, "[范围赋值] assign范围赋值");
}
// 2.6 初始化列表赋值
void demo_assign_initializer_list()
{
std::forward_list<int> fl;
fl.assign({ 5, 10, 15, 20 });
print_forward_list(fl, "[初始化列表赋值] assign初始化列表赋值");
}
// =======================================================
/*
* 3. 迭代器
*/
// 3.1 before_begin
void demo_before_begin()
{
std::forward_list<int> fl = { 1, 2, 3 };
auto it = fl.before_begin();
fl.insert_after(it, 0);
print_forward_list(fl, "[before_begin] 插入0");
}
// 3.2 begin / end
void demo_begin_end()
{
std::forward_list<int> fl = { 10, 20, 30 };
std::cout << "[正向迭代] begin到end遍历: ";
for (auto it = fl.begin(); it != fl.end(); it++)
{
std::cout << *it << " ";
}
std::cout << std::endl;
}
// 3.3 cbegin / cend
void demo_cbegin_cend()
{
std::forward_list<int> fl = { 10, 20, 30 };
std::cout << "[const正向迭代] cbegin到cend遍历(只读): ";
for (auto it = fl.cbegin(); it != fl.cend(); it++)
{
std::cout << *it << " ";
}
std::cout << std::endl;
}
// 3.4 cbefore_begin
void demo_cbefore_begin()
{
std::forward_list<int> fl = { 1, 2, 3 };
auto it = fl.cbefore_begin();
auto next = std::next(it);
std::cout << "[cbefore_begin] 下一个元素: " << *next << std::endl;
}
// =======================================================
/*
* 4. 容量查询
*/
// 4.1 empty
void demo_empty()
{
std::forward_list<int> fl1;
std::forward_list<int> fl2 = { 1, 2, 3 };
std::cout << "[empty] 空列表 fl1: " << fl1.empty() << std::endl;
std::cout << "[empty] 非空列表 fl2: " << fl2.empty() << std::endl;
}
// 4.2 max_size
void demo_max_size()
{
std::forward_list<int> fl;
std::cout << "[max_size] forward_list<int>: " << fl.max_size() << std::endl;
std::forward_list<Student> fl2;
std::cout << "[max_size] forward_list<Student>: " << fl2.max_size() << std::endl;
}
// =======================================================
/*
* 5. 元素访问
*/
// 5.1 front
void demo_front()
{
std::forward_list<int> fl = { 10, 20, 30 };
std::cout << "[front] 修改前front: " << fl.front() << std::endl;
fl.front() = 99;
print_forward_list(fl, "[front] 修改front后:");
}
void demo_front_custom()
{
std::forward_list<Student> fl = {
Student("Alice", 95.5),
Student("Bob", 88.0)
};
std::cout << "[front] 自定义类型front: " << fl.front() << std::endl;
}
// =======================================================
/*
* 6. 修改器 - 头部操作
*/
// 6.1 push_front
void demo_push_front()
{
std::forward_list<int> fl = { 2, 3, 4 };
fl.push_front(1);
fl.push_front(0);
print_forward_list(fl, "[push_front]");
}
// 6.2 emplace_front
void demo_emplace_front()
{
std::forward_list<Student> fl;
fl.emplace_front("Alice", 95.5);
fl.emplace_front("Bob", 88.0);
print_student_list(fl, "[emplace_front]");
}
// 6.3 pop_front
void demo_pop_front()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.pop_front();
fl.pop_front();
print_forward_list(fl, "[pop_front] 两次后");
}
// =======================================================
/*
* 7. 修改器 - insert_after
*/
// 7.1 insert_after单元素
void demo_insert_after_single()
{
std::forward_list<int> fl = { 1, 3, 5 };
auto it = fl.begin();
fl.insert_after(it, 2);
print_forward_list(fl, "[insert_after单元素] 在第一个元素后插入2");
}
// 7.2 insert_after填充
void demo_insert_after_fill()
{
std::forward_list<int> fl = { 1, 5 };
auto it = fl.begin();
fl.insert_after(it, 3, 3);
print_forward_list(fl, "[insert_after填充] 在第一个元素后插入3个3");
}
// 7.3 insert_after范围
void demo_insert_after_range()
{
std::forward_list<int> fl = { 1, 10 };
int arr[] = { 3, 5, 7 };
auto it = fl.begin();
fl.insert_after(it, std::begin(arr), std::end(arr));
print_forward_list(fl, "[insert_afterf范围] 在第一个元素后插入范围");
}
// 7.6 insert_after初始化列表
void demo_insert_after_initializer_list()
{
std::forward_list<int> fl = { 1, 10 };
auto it = fl.begin();
fl.insert_after(it, { 2, 4, 6, 8 });
print_forward_list(fl, "[insert_after初始化列表] 在第一个元素后插入初始化列表");
}
// 7.5 insert_after / before_begin
void demo_insert_after_before_begin()
{
std::forward_list<int> fl = { 3, 4, 5 };
auto it = fl.before_begin();
fl.insert_after(it, 1);
fl.insert_after(it, 2);
print_forward_list(fl, "[insert_after] before_begin后连续插入");
}
// 7.6 insert_after自定义类型
void demo_insert_after_custom()
{
std::forward_list<Student> fl = { Student("Bob", 88.0) };
auto it = fl.before_begin();
fl.insert_after(it, Student("Alice", 95.5));
print_student_list(fl, "[insert_after] 自定义类型");
}
// =======================================================
/*
* 8. 修改器 - emplace_after
*/
// 8.1 emplace_after 自定义类型
void demo_emplace_after()
{
std::forward_list<Student> fl;
auto it = fl.before_begin();
it = fl.emplace_after(it, "Charlie", 90.0);
it = fl.emplace_after(it, "Bob", 88.0);
fl.emplace_after(fl.before_begin(), "Alice", 95.5);
print_student_list(fl, "[emplace_after] 自定义类型 ");
}
// 8.2 emplace_after int
void demo_emplace_after_int()
{
std::forward_list<int> fl = { 1, 5 };
auto it = fl.begin();
fl.emplace_after(it, 3);
print_forward_list(fl, "[emplace_after] int");
}
// =======================================================
/*
* 9. 修改器 - erase_after
*/
// 9.1 erase_after单元素
void demo_erase_after_single()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
auto it = fl.begin();
fl.erase_after(it);
print_forward_list(fl, "[erase_after单元素] 删除第一个元素之后");
}
// 9.2 erase_after范围
void demo_erase_after_range()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5, 6 };
auto first = fl.begin();
auto last = std::next(first, 4);
fl.erase_after(first, last);
print_forward_list(fl, "[erase_after范围] 范围删除");
}
// 9.3 erase_after头部
void demo_erase_after_head()
{
std::forward_list<int> fl = { 10, 20, 30 };
fl.erase_after(fl.before_begin());
print_forward_list(fl, "[erase_after头部] 删除头部(before_begin之后)");
}
// =======================================================
/*
* 10. 修改器 - swap / resie / clear
*/
// 10.1 swap
void demo_swap()
{
std::forward_list<int> fl1 = { 1, 2, 3 };
std::forward_list<int> fl2 = { 10, 20, 30, 40 };
fl1.swap(fl2);
print_forward_list(fl1, "[swap] 后fl1");
print_forward_list(fl2, "[swap] 后fl2");
}
// 10.2 resize缩小
void demo_resize_smaller()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.resize(3);
print_forward_list(fl, "[resize] 缩小到3");
}
// 10.3 resize扩大
void demo_resize_larger()
{
std::forward_list<int> fl = { 1, 2, 3 };
fl.resize(6);
print_forward_list(fl, "[resize] 扩大到6(默认值0)");
}
// 10.3 resize扩大填充
void demo_resize_larger_value()
{
std::forward_list<int>fl = { 1, 2, 3 };
fl.resize(6, 99);
print_forward_list(fl, "[resize] 扩大到6(填充99)");
}
// 10.4 clear
void demo_clear()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.clear();
print_forward_list(fl, "[clear] 后");
std::cout << "[clear] 后empty: " << fl.empty() << std::endl;
}
// =======================================================
/*
* 11 操作 - merge
*/
// 11.1 merge
void demo_merge()
{
std::forward_list<int> fl1 = { 1, 3, 5, 7 };
std::forward_list<int> fl2 = { 2, 4, 6, 8 };
fl1.merge(fl2);
print_forward_list(fl1, "[merge] 后fl1");
print_forward_list(fl2, "[merge] 后fl2(已空)");
}
// 11.2 merge比较器
void demo_merge_custom_comparator()
{
std::forward_list<int> fl1 = { 7, 5, 3, 1 };
std::forward_list<int> fl2 = { 8, 6, 4, 2 };
fl1.merge(fl2, std::greater<int>());
print_forward_list(fl1, "[merge] 降序后fl1");
}
// 11.3 merge自定义类型
void demo_merge_student()
{
std::forward_list<Student> fl1 = {
Student("Charlie", 88.0),
Student("Alice", 95.5)
};
std::forward_list<Student> fl2 = {
Student("Dave", 90.1),
Student("Bob", 92.3)
};
// 按升序merge,要确保两个对象都是已经按升序sort的,否则会报错
fl1.merge(fl2, [](const Student& a, const Student& b) {
return a.getScore() < b.getScore();
});
print_student_list(fl1, "[merge自定义类型] 按分数merge后fl1");
}
// =======================================================
/*
* 12. 操作 - splice_after
*/
// 12.1 splice_after全部
void demo_splice_after_all()
{
std::forward_list<int> fl1 = { 1, 2 };
std::forward_list<int> fl2 = { 10, 20, 30 };
fl1.splice_after(fl1.before_begin(), fl2);
print_forward_list(fl1, "[splice_after全部] 后fl1");
print_forward_list(fl2, "[splice_after全部] 后fl2(已空)");
}
// 12.2 splice_after单元素
void demo_splice_after_single()
{
std::forward_list<int> fl1 = { 1, 2 };
std::forward_list<int> fl2 = { 10, 20, 30 };
auto it = fl1.begin();
auto it2 = fl2.begin();
fl1.splice_after(it, fl2, it2);
print_forward_list(fl1, "[splice_after单元素] 后fl1");
print_forward_list(fl2, "[splice_after单元素] 后fl2");
}
// 12.3 splice_after范围
void demo_splice_after_range()
{
std::forward_list<int> fl1 = { 1, 5 };
std::forward_list<int> fl2 = { 10, 20, 30, 40, 50 };
auto pos = fl1.begin();
auto first = fl2.before_begin();
auto last = std::next(first, 3);
fl1.splice_after(pos, fl2, first, last);
print_forward_list(fl1, "[splice_after范围] 后fl1");
print_forward_list(fl2, "[splice_after范围] 后fl2");
}
// =======================================================
/*
* 13. 操作 - remove / remove_if
*/
// 13.1 remove
void demo_remove()
{
std::forward_list<int> fl = { 1, 2, 3, 2, 4, 2, 5 };
fl.remove(2);
print_forward_list(fl, "[remove] 2后");
}
// 13.2 remove_if
void demo_remove_if()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
fl.remove_if([](int x) { return x % 2 == 0; });
print_forward_list(fl, "[remove_if] 偶数后");
}
// 13.3 remove自定义类型
void demo_remove_student()
{
std::forward_list<Student> fl = {
Student("Alice", 95.5),
Student("Bob", 55.0),
Student("Charlie", 88.0),
Student("Dave", 40.0)
};
fl.remove_if([](const Student& s) { return s.getScore() < 60.0; });
print_student_list(fl, "[remove_if] 不及格后");
}
// =======================================================
/*
* 14. 操作 - reverse
*/
// 14.1 reverse
void demo_reverse()
{
std::forward_list<int> fl = { 1, 2, 3, 4, 5 };
fl.reverse();
print_forward_list(fl, "[reverse] 后fl");
}
// 14.2 reverse自定义类型
void demo_reverse_student()
{
std::forward_list<Student> fl = {
Student("Alice", 95.5),
Student("Bob", 88.0),
Student("Charlie", 92.3)
};
fl.reverse();
print_student_list(fl, "[reverse自定义类型] 后fl");
}
// =======================================================
/*
* 15. 操作 - unique
*/
// 15.1 unique
void demo_unique()
{
std::forward_list<int> fl = { 1, 1, 2, 2, 2, 3, 3, 4, 1, 1 };
fl.unique();
print_forward_list(fl, "[unique] 连续重复去重");
}
// 15.2 unique自定义去重
void demo_unique_custom_pred()
{
std::forward_list<int> fl = { 1, -1, 2, -2, 3, 3, 4, -4 };
fl.unique([](int a, int b) { return std::abs(a) == std::abs(b); });
print_forward_list(fl, "[unique] 绝对值相同去重后");
}
// 15.3 unique自定义类型
void demo_unique_student()
{
std::forward_list<Student> fl = {
Student("Alice", 95.5),
Student("Alice", 95.5),
Student("Bob", 88.0),
Student("Bob", 90.0)
};
fl.unique([](const Student& a, const Student& b) { return a.getName() == b.getName(); });
print_student_list(fl, "[unique] 同名去重");
}
// =======================================================
/*
* 16. 操作 - sort
*/
// 16.1 sort
void demo_sort()
{
std::forward_list<int> fl = {5, 3, 1, 4, 2};
fl.sort();
print_forward_list(fl, "[sort] 升序后");
}
// 16.2 sort降序
void demo_sort_descending()
{
std::forward_list<int> fl = { 5, 3, 1, 4, 2 };
fl.sort(std::greater<int>());
print_forward_list(fl, "[sort] 降序后");
}
// 16.3 sort自定义类型
void demo_sort_student()
{
std::forward_list<Student> fl = {
Student("Charlie", 92.3),
Student("Alice", 95.5),
Student("Bob", 88.0),
Student("Dave", 90.1)
};
fl.sort([](const Student& a, const Student& b) {
return a.getScore() > b.getScore();
});
print_student_list(fl, "[sort] 按分数降序后");
}
// =======================================================
/*
* 17. 非成员函数
*/
// 17.1 比较操作符
void demo_comparsion()
{
std::forward_list<int> fl1 = { 1, 2, 3 };
std::forward_list<int> fl2 = { 1, 2, 3 };
std::forward_list<int> fl3 = { 1, 2, 4 };
std::cout << "fl1 == fl2: " << (fl1 == fl2) << std::endl;
std::cout << "fl1 != fl3: " << (fl1 != fl3) << std::endl;
std::cout << "fl1 < fl3: " << (fl1 < fl3) << std::endl;
std::cout << "fl3 > fl1: " << (fl3 > fl1) << std::endl;
}
// 17.2 std::swap
void demo_std_swap()
{
std::forward_list<int> fl1 = { 1, 2 };
std::forward_list<int> fl2 = { 10, 20 };
std::swap(fl1, fl2);
print_forward_list(fl1, "[std::swap] 后fl1");
print_forward_list(fl2, "[std::swap] 后fl2");
}
// 17.3 std::erase
void demo_std_erase()
{
#if _MSVC_LANG >= 202002L
std::forward_list<int> fl = { 1, 2, 3, 2, 4, 2, 5 };
std::erase(fl, 2);
print_forward_list(fl, "[std::erase] 2后");
#endif
}
// 17.4 std::erase_if
void demo_std_erase_if()
{
#if _MSVC_LANG >= 202002L
std::forward_list<int> fl = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::erase_if(fl, [](int x) { return x > 5; });
print_forward_list(fl, "[std::erase_if] >5后");
#endif
}
// =======================================================
/*
* 18. 综合应用示例
*/
void demo_comprehensive_usage()
{
// 构建一个学生成绩管理系统
std::forward_list<Student> students;
// 1. 批量添加学生
students.emplace_front("Frank", 45.0);
students.emplace_front("Dave", 90.1);
students.emplace_front("Charlie", 92.3);
students.emplace_front("Bob", 88.0);
students.emplace_front("Alice", 95.5);
print_student_list(students, "初始学生列表");
// 2. 在指定位置插入
auto it = students.begin();
std::advance(it, 1);
students.emplace_after(it, "Eve", 91.0);
print_student_list(students, "插入Eve后");
// 3. 删除不及格的学生
students.remove_if([](const Student& s) { return s.getScore() < 60.0; });
print_student_list(students, "删除不及格后");
// 4. 按分数排序
students.sort([](const Student& a, const Student& b) {
return a.getScore() > b.getScore();
});
print_student_list(students, "按分数降序排序后");
// 5. 反转
students.reverse();
print_student_list(students, "反转后(分数从低到高)");
// 6. 合并另一班级
std::forward_list<Student> students2 = {
Student("Grace", 87.5),
Student("Henry", 93.0)
};
// 先对两个列表排序再合并
students.sort([](const Student& a, const Student& b) {
return a.getScore() < b.getScore();
});
students2.sort([](const Student& a, const Student& b) {
return a.getScore() < b.getScore();
});
students.merge(students2, [](const Student& a, const Student& b) {
return a.getScore() < b.getScore();
});
print_student_list(students, "合并两班后(按分数升序)");
}
// =======================================================
int main()
{
std::cout << "========== 1. 析构函数 ==========" << std::endl;
demo_default_constructor();
demo_fill_constructor_value();
demo_fill_constructor_default();
demo_range_constructor();
demo_initializer_list_constructor();
demo_copy_constructor();
demo_move_constructor();
demo_custom_type_constructor();
std::cout << "\n========== 2. 赋值操作 ==========" << std::endl;
demo_copy_assignment();
demo_move_assignment();
demo_initializer_list_assignment();
demo_assign_fill();
demo_assign_range();
demo_assign_initializer_list();
std::cout << "\n========== 3. 迭代器 ==========" << std::endl;
demo_before_begin();
demo_begin_end();
demo_cbegin_cend();
demo_cbefore_begin();
std::cout << "\n========== 4. 容量查询 ==========" << std::endl;
demo_empty();
demo_max_size();
std::cout << "\n========== 5. 元素访问 ==========" << std::endl;
demo_front();
demo_front_custom();
std::cout << "\n========== 6. 修改器 - 头部操作 ==========" << std::endl;
demo_push_front();
demo_emplace_front();
demo_pop_front();
std::cout << "\n========== 7. 修改器 - insert_after ==========" << std::endl;
demo_insert_after_single();
demo_insert_after_fill();
demo_insert_after_range();
demo_insert_after_initializer_list();
demo_insert_after_before_begin();
demo_insert_after_custom();
std::cout << "\n========== 8. 修改器 - emplace_after ==========" << std::endl;
demo_emplace_after();
demo_emplace_after_int();
std::cout << "\n========== 9. 修改器 - erase_after ==========" << std::endl;
demo_erase_after_single();
demo_erase_after_range();
demo_erase_after_head();
std::cout << "\n========== 10. 修改器 - swap / resize / clear ==========" << std::endl;
demo_swap();
demo_resize_smaller();
demo_resize_larger();
demo_resize_larger_value();
demo_clear();
std::cout << "\n========== 11. 操作 - merge ==========" << std::endl;
demo_merge();
demo_merge_custom_comparator();
demo_merge_student();
std::cout << "\n========== 12. 操作 - splice_after ==========" << std::endl;
demo_splice_after_all();
demo_splice_after_single();
demo_splice_after_range();
std::cout << "\n========== 13. 操作 - remove / remove_if ==========" << std::endl;
demo_remove();
demo_remove_if();
demo_remove_student();
std::cout << "\n========== 14. 操作 - reverse ==========" << std::endl;
demo_reverse();
demo_reverse_student();
std::cout << "\n========== 15. 操作 - unique ==========" << std::endl;
demo_unique();
demo_unique_custom_pred();
demo_unique_student();
std::cout << "\n========== 16. 操作 - sort ==========" << std::endl;
demo_sort();
demo_sort_descending();
demo_sort_student();
std::cout << "\n========== 17. 非成员函数 ==========" << std::endl;
demo_comparsion();
demo_std_swap();
demo_std_erase();
demo_std_erase_if();
std::cout << "\n========== 18. 综合应用示例 ==========" << std::endl;
demo_comprehensive_usage();
return 0;
}