C++学习笔记STL(Standard Template Library)标准模板库-顺序容器(forward_list单链表)
std::forward_list
是支持从容器中的任何位置快速插入和移除元素的容器。不支持快速随机访问。它实现为单链表,且实质上与其在C中实现相比无任何开销。与,std::list相比,此容器在不需要双向迭代时提供更有效地利用空间的存储。
一,元素访问
1.front 访问第一个元素;
二,迭代器
1.before_begin/cbefore_begin 返回指向第一个元素之前迭代器
2.begin/cbegin 返回指向起始的迭代器
3.end/cend 返回指向未尾的迭代器
1 forward_list<char> letters{ 'o', 'm', 'g', 'w', 't', 'f' }; 2 3 if (!letters.empty()) { 4 cout << "The first character is: " << letters.front() << '\n'; 5 } 6 7 8 forward_list<int> nums{ 1, 2, 4, 8, 16 }; 9 forward_list<std::string> fruits{ "orange", "apple", "raspberry" }; 10 forward_list<char> empty; 11 12 // 求和 forward_list nums 中的所有整数(若存在),仅打印结果。 13 cout << "Sum of nums: " << 14 accumulate(nums.begin(), nums.end(), 0) << "\n"; 15 16 // 打印 forward_list fruits 中的首个 fruis ,不检查是否有一个。 17 cout << "First fruit: " << *fruits.begin() << "\n"; 18 19 if (empty.begin() == empty.end()) 20 cout << "forward_list 'empty' is indeed empty.\n";
三,容量
1.empty 检查容器是否为空
2.max_size 返回可容纳的最大元素数
四,修改器
1.clear 清除内容
2.insert_after 在某个元素后插入新元素
1 template<typename T> 2 std::ostream& operator<<(std::ostream& s, const std::forward_list<T>& v) 3 { 4 s.put('['); 5 char comma[3] = { '\0', ' ', '\0' }; 6 for (const auto& e : v) { 7 s << comma << e; 8 comma[0] = ','; 9 } 10 return s << ']'; 11 } 12 13 int main(){ 14 std::forward_list<std::string> words{ "the", "frogurt", "is", "also", "cursed" }; 15 std::cout << "words: " << words << '\n'; 16 // insert_after (2) 17 auto beginIt = words.begin(); 18 words.insert_after(beginIt, "strawberry"); 19 std::cout << "words: " << words << '\n'; 20 // insert_after (3) 21 auto anotherIt = beginIt; 22 ++anotherIt; 23 anotherIt = words.insert_after(anotherIt, 2, "strawberry"); 24 std::cout << "words: " << words << '\n'; 25 // insert_after (4) 26 vector<std::string> V = { "apple", "banana", "cherry" }; 27 anotherIt = words.insert_after(anotherIt, V.begin(), V.end()); 28 std::cout << "words: " << words << '\n'; 29 // insert_after (5) 30 words.insert_after(anotherIt, { "jackfruit", "kiwifruit", "lime", "mango" }); 31 std::cout << "words: " << words << '\n'; 32 33 }
3.emplace_after 在元素后原位置构造元素
1 struct Sum { 2 std::string remark; 3 int sum; 4 5 Sum(std::string remark, int sum) 6 : remark{ std::move(remark) }, sum{ sum } {} 7 8 void print() const { 9 std::cout << remark << " = " << sum << '\n'; 10 } 11 }; 12 std::forward_list<Sum> list; 13 auto iter = list.before_begin(); 14 std::string str{ "1" }; 15 for (int i{ 1 }, sum{ 1 }; i != 10; sum += i) { 16 iter = list.emplace_after(iter, str, sum); 17 ++i; 18 str += " + " + std::to_string(i); 19 } 20 21 for (const Sum& s : list) s.print();
4.erase_after 擦除元素后的元素
5.push_front 插入元素到容器起始
6.emplace_front在容器头部就地构造元素
7.pop_front 移除首元素
8.resize 改变容器中可存储元素的个数
9.swap 交换内容
五,操作
1.merge 合并两个已排序列表
1 std::ostream& operator<<(std::ostream& ostr, const std::forward_list<int>& list) 2 { 3 for (auto& i : list) { 4 ostr << " " << i; 5 } 6 return ostr; 7 } 8 std::forward_list<int> list1 = { 5,9,0,1,3 }; 9 std::forward_list<int> list2 = { 8,7,2,6,4 }; 10 list1.sort(); 11 list2.sort(); 12 std::cout << "list1: " << list1 << "\n"; 13 std::cout << "list2: " << list2 << "\n"; 14 list1.merge(list2); 15 std::cout << "merged: " << list1 << "\n";
2.splice_after 从另一forward_list移动元素
3.remove/remove_if 移除满足特定标准的元素
1 std::forward_list<int> l = { 1,100,2,3,10,1,11,-1,12 }; 2 l.remove(1); // 移除两个等于 1 的元素 3 l.remove_if([](int n) { return n > 10; }); // 移除全部大于 10 的元素 4 for (int n : l) { 5 std::cout << n << ' '; 6 } 7 std::cout << '\n';
4.reverse 将该链表的所有元素的顺序反转
1 std::forward_list<int> list = { 8,7,5,9,0,1,3,2,6,4 }; 2 std::cout << "before: " << list << "\n"; 3 list.sort(); 4 std::cout << "ascending: " << list << "\n"; 5 list.reverse(); 6 std::cout << "descending: " << list << "\n";
5.unique 删除连续的重复元素
1 std::forward_list<int> x = {1, 2, 2, 3, 3, 2, 1, 1, 2}; 2 3 std::cout << "contents before:"; 4 for (auto val : x) 5 std::cout << ' ' << val; 6 std::cout << '\n'; 7 8 x.unique(); 9 std::cout << "contents after unique():"; 10 for (auto val : x) 11 std::cout << ' ' << val; 12 std::cout << '\n'; 13
6.sort 对元素进行排序
1 std::forward_list<int> list = { 8,7,5,9,0,1,3,2,6,4 }; 2 3 std::cout << "before: " << list << "\n"; 4 list.sort(); 5 std::cout << "ascending: " << list << "\n"; 6 list.sort(std::greater<int>()); 7 std::cout << "descending: " << list << "\n";