目录
list是什么
构造函数
元素访问
容量操作
修改
迭代器
code实例
实现简单的list
forward_list是什么
构造函数
元素访问
容量
修改
迭代器
code实例
实现一个简单的forward_list
list是什么
std::list 是 C++ 标准模板库(STL)中的一个双向链表容器。它支持在任意位置高效地插入和删除元素,但随机访问性能较差。底层结构:数据域:存储元素的值。前驱指针:指向前一个节点。 后继指针:指向后一个节点。优点:在任意位置插入和删除元素的时间复杂度为 O(1)。不需要连续内存,适合频繁插入和删除的场景。缺点:随机访问的时间复杂度为 O(n)。内存开销较大,因为每个节点需要额外存储两个指针。
struct ListNode {T value; // 数据域ListNode* prev; // 前驱指针ListNode* next; // 后继指针
};
构造函数
list() | 默认构造函数,创建一个空的 list |
list(size_type count) | 创建包含 count 个默认初始化元素的 list。 |
list(size_type count, const T& value) | 创建包含 count 个值为 value 的元素的 list |
list(const list& other) | 拷贝构造函数 |
list(initializer_list<T> init) | 使用初始化列表创建 list |
元素访问
front(): 返回第一个元素的引用。back(): 返回最后一个元素的引用。
容量操作
empty(): 检查否为空。size(): 返回 list 中的元素数量。max_size(): 返回 list 可以容纳的最大元素数量。
修改
clear(): 清除 list 中的所有元素 | erase(iterator pos): 删除指定位置 pos 处的元素 |
pop_back(): 删除 list 的最后一个元素 | pop_front(): 删除 list 的第一个元素 |
swap(list& other): 交换两个 list 的内容 | resize(size_type count): 改变 list 的大小为 count |
insert(iterator pos, const T& value): 在指定位置 pos 插入元素 value | |
push_back(const T& value): 在 list 的末尾添加元素 value | |
push_front(const T& value): 在 list 的开头添加元素 value |
迭代器
begin(), end(): 返回指向第一个元素和最后一个元素之后位置的迭代器 | |
rbegin(), rend(): 返回反向迭代器 | |
cbegin(), cend(), crbegin(), crend(): 返回常量迭代器 |
std::list 的迭代器是一个双向迭代器,支持 ++ 和 -- 操作。迭代器的实现通常是对链表节点的封装,erase:删除元素时,指向被删除元素的迭代器失效。insert:插入元素不会使其他迭代器失效。push_back 和 push_front:也不会使其他迭代器失效。
code实例
#include <list>
#include <iostream>int main() {//构造函数std::list<int> l1; // 空 liststd::list<int> l2(5); // 包含 5 个默认初始化元素的 liststd::list<int> l3(5, 10); // 包含 5 个值为 10 的元素的 liststd::list<int> l = {1, 2, 3, 4, 5}; // 使用初始化列表创建 list//元素访问std::cout << "l.front(): " << l.front() << std::endl; // 输出: 1std::cout << "l.back(): " << l.back() << std::endl; // 输出: 5//容量std::cout << "l.empty(): " << l.empty() << std::endl; // 输出: 0 (false)std::cout << "l.size(): " << l.size() << std::endl; // 输出: 5std::cout << "l.max_size(): " << l.max_size() << std::endl; // 输出: 一个很大的数std::cout << std::endl;//修改l.push_back(6); // l: {1, 2, 3, 4, 5, 6}l.push_front(0); // l: {0, 1, 2, 3, 4, 5, 6}l.pop_back(); // l: {0, 1, 2, 3, 4, 5}l.pop_front(); // l: {1, 2, 3, 4, 5}l.insert(std::next(l.begin(), 2), 10); // l: {1, 2, 10, 3, 4, 5}l.erase(std::next(l.begin(), 3)); // l: {1, 2, 10, 4, 5}for (int i : l) {std::cout << i << " "; // 输出: 1 2 10 4 5}//迭代器std::cout << "l: ";for (auto it = l.begin(); it != l.end(); ++it) {std::cout << *it << " "; // 输出: 1 2 10 4 5}std::cout << std::endl;std::cout << "l (reverse): ";for (auto it = l.rbegin(); it != l.rend(); ++it) {std::cout << *it << " "; // 输出: 5 4 10 2 1}return 0;
}
实现简单的list
#include <iostream>template <typename T>
class SimpleList {
private:struct Node {T value;Node* prev;Node* next;Node(const T& val) : value(val), prev(nullptr), next(nullptr) {}};Node* head;Node* tail;size_t size;public:SimpleList() : head(nullptr), tail(nullptr), size(0) {}~SimpleList() {while (head) {Node* temp = head;head = head->next;delete temp;}}void push_back(const T& value) {Node* newNode = new Node(value);if (!tail) {head = tail = newNode;} else {tail->next = newNode;newNode->prev = tail;tail = newNode;}size++;}void print() const {Node* current = head;while (current) {std::cout << current->value << " ";current = current->next;}std::cout << std::endl;}
};int main() {SimpleList<int> list;list.push_back(1);list.push_back(2);list.push_back(3);list.print(); // 输出: 1 2 3return 0;
}
forward_list是什么
std::forward_list 是 C++ 标准模板库(STL)中的一个单向链表容器。它支持在链表头部高效地插入和删除元素,但不支持反向遍历。每个节点包含:数据域:存储元素的值。后继指针:指向下一个节点。在链表头部插入和删除元素的时间复杂度为 O(1)。内存开销比 std::list 更小,因为每个节点只需要一个指针。随机访问的时间复杂度为 O(n)
struct ForwardListNode {T value; // 数据域ForwardListNode* next; // 后继指针
};
构造函数
forward_list() | 默认构造函数,创建一个空的 forward_list |
forward_list(size_type count) | 创建包含 count 个默认初始化元素的 forward_list |
forward_list(size_type count, const T& value) | 创建包含 count 个值为 value 的元素的 forward_list |
forward_list(const forward_list& other) | 拷贝构造函数 |
forward_list(initializer_list<T> init) | 使用初始化列表创建 forward_list |
元素访问
front(): 返回第一个元素的引用
容量
empty(): 检查 forward_list 是否为空。max_size(): 返回 forward_list 可以容纳的最大元素数量
修改
clear(): 清除 forward_list 中的所有元素 | pop_front(): 删除 forward_list 的第一个元素 |
erase_after(iterator pos): 删除指定位置pos之后的元素 | swap(forward_list& other): 交换两个链表的内容 |
insert_after(iterator pos, const T& value): 在指定位置 pos 之后插入元素 value | |
push_front(const T& value): 在 forward_list 的开头添加元素 value | |
resize(size_type count): 改变 forward_list 的大小为 count |
迭代器
before_begin(): 返回指向第一个元素之前位置的迭代器 |
begin(), end(): 返回指向第一个元素和最后一个元素之后位置的迭代器 |
cbefore_begin(), cbegin(), cend(): 返回常量迭代器 |
std::forward_list 的迭代器是一个前向迭代器,仅支持 ++ 操作。迭代器的实现通常是对链表节点的封装。erase_after:删除元素时,指向被删除元素的迭代器失效。insert_after/push_front:不会使其他迭代器失效。
code实例
#include <forward_list>
#include <iostream>int main() {//构造函数std::forward_list<int> fl1; // 空 forward_liststd::forward_list<int> fl2(5); // 包含 5 个默认初始化元素的 forward_liststd::forward_list<int> fl3(5, 10); // 包含 5 个值为 10 的元素的 forward_liststd::forward_list<int> fl = {1, 2, 3, 4, 5}; // 使用初始化列表创建 forward_list//元素访问std::cout << "fl.front(): " << fl.front() << std::endl; // 输出: 1//容量操作std::cout << "fl.empty(): " << fl.empty() << std::endl; // 输出: 0 (false)std::cout << "fl.max_size(): " << fl.max_size() << std::endl; // 输出: 一个很大的数//修改fl.push_front(0); // fl: {0, 1, 2, 3, 4, 5}fl.pop_front(); // fl: {1, 2, 3, 4, 5}fl.insert_after(fl.before_begin(), 10); // fl: {10, 1, 2, 3, 4, 5}fl.erase_after(fl.before_begin()); // fl: {1, 2, 3, 4, 5}for (auto it = fl.begin(); it != fl.end(); ++it) {std::cout << *it << " "; // 输出: 1 2 3 4 5}return 0;
}
实现一个简单的forward_list
#include <iostream>template <typename T>
class SimpleForwardList {
private:struct Node {T value;Node* next;Node(const T& val) : value(val), next(nullptr) {}};Node* head;size_t size;public:SimpleForwardList() : head(nullptr), size(0) {}~SimpleForwardList() {while (head) {Node* temp = head;head = head->next;delete temp;}}void push_front(const T& value) {Node* newNode = new Node(value);newNode->next = head;head = newNode;size++;}void print() const {Node* current = head;while (current) {std::cout << current->value << " ";current = current->next;}std::cout << std::endl;}
};int main() {SimpleForwardList<int> fl;fl.push_front(1);fl.push_front(2);fl.push_front(3);fl.print(); // 输出: 3 2 1return 0;
}