C++ 参考手册
- C++11
 - C++14
 - C++17
 - C++20
 - C++ 编译器支持情况表
 - 独立与宿主实现
 - C++ 语言
 - C++ 关键词
 - 预处理器
 - C++ 标准库头文件
 - 具名要求
 - 功能特性测试 (C++20)
 - 工具库
 - 类型支持(基本类型、RTTI、类型特性)
 - 概念库 (C++20)
 - 错误处理
 - 动态内存管理
 - 日期和时间工具
 - 字符串库
 - 容器库
 - std::array
 - std::vector
 - std::map
 - std::unordered_map
 - std::priority_queue
 - std::span
 - std::forward_list
 - std::forward_list<T,Allocator>::forward_list
 - std::forward_list<T,Allocator>::~forward_list
 - std::forward_list<T,Allocator>::operator=
 - std::forward_list<T,Allocator>::unique
 - std::forward_list<T,Allocator>::reverse
 - std::forward_list<T,Allocator>::sort
 - std::forward_list 的推导指引
 - std::forward_list<T,Allocator>::merge
 - std::forward_list<T,Allocator>::splice_after
 - std::forward_list<T,Allocator>::remove, remove_if
 - operator==,!=,<,<=,>,>=,<=>(std::forward_list)
 - std::swap(std::forward_list)
 - std::erase, std::erase_if (std::forward_list)
 - std::forward_list<T,Allocator>::swap
 - std::forward_list<T,Allocator>::erase_after
 - std::forward_list<T,Allocator>::push_front
 - std::forward_list<T,Allocator>::emplace_front
 - std::forward_list<T,Allocator>::pop_front
 - std::forward_list<T,Allocator>::resize
 - std::forward_list<T,Allocator>::empty
 - std::forward_list<T,Allocator>::max_size
 - std::forward_list<T,Allocator>::clear
 - std::forward_list<T,Allocator>::insert_after
 - std::forward_list<T,Allocator>::emplace_after
 - std::forward_list<T,Allocator>::begin, std::forward_list<T,Allocator>::cbegin
 - std::forward_list<T,Allocator>::end, std::forward_list<T,Allocator>::cend
 - std::forward_list<T,Allocator>::assign
 - std::forward_list<T,Allocator>::get_allocator
 - std::forward_list<T,Allocator>::front
 - std::forward_list<T,Allocator>::before_begin, cbefore_begin
 - std::deque
 - std::list
 - std::set
 - std::multiset
 - std::multimap
 - std::unordered_set
 - std::unordered_multiset
 - std::unordered_multimap
 - std::stack
 - std::queue
 - std::vector<bool>
 - 结点把柄 (C++17)
 - 注释
 - 迭代器库
 - 范围库 (C++20)
 - 算法库
 - 数值库
 - 输入/输出库
 - 文件系统库
 - 本地化库
 - 正则表达式库
 - 原子操作库
 - 线程支持库
 - 实验性 C++ 特性
 - 有用的资源
 - 索引
 - std 符号索引
 - 协程支持 (C++20)
 - C++ 关键词
 
位置:首页 > C++ 参考手册 >容器库 >std::forward_list > std::forward_list<T,Allocator>::merge
std::forward_list<T,Allocator>::merge
|   void merge( forward_list& other );  | 
(1) | (C++11 起) | 
|   void merge( forward_list&& other );  | 
(1) | (C++11 起) | 
|   template <class Compare>  void merge( forward_list& other, Compare comp );  | 
(2) | (C++11 起) | 
|   template <class Compare>  void merge( forward_list&& other, Compare comp );  | 
(2) | (C++11 起) | 
归并二个已排序链表为一个。链表应以升序排序。
不复制元素。操作后容器 other 变为空。若 other 与 *this 指代同一对象则函数不做任何事。若 get_allocator() != other.get_allocator() ,则行为未定义。没有引用和迭代器变得非法,除了被移动元素的迭代器现在指代到 *this 中,而非到 other 中,第一版本用 operator< 比较元素,第二版本用给定的比较函数 comp 。
此操作是稳定的:对于二个链表中的等价元素,来自 *this 的元素始终前驱来自 other 的元素,而且 *this 和 other 的等价元素顺序不更改。
参数
| other | - | 要交换的另一容器 | 
| comp | - |  比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true 。 比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型   | 
返回值
(无)
异常
若抛出异常,则此函数无效果(强异常保证),除非异常来自比较函数。
复杂度
至多 std::distance(begin(), end()) + std::distance(other.begin(), other.end()) - 1 次比较。
示例
#include <iostream> #include <forward_list> std::ostream& operator<<(std::ostream& ostr, const std::forward_list<int>& list) { for (auto &i : list) { ostr << " " << i; } return ostr; } int main() { std::forward_list<int> list1 = { 5,9,0,1,3 }; std::forward_list<int> list2 = { 8,7,2,6,4 }; list1.sort(); list2.sort(); std::cout << "list1: " << list1 << "\n"; std::cout << "list2: " << list2 << "\n"; list1.merge(list2); std::cout << "merged: " << list1 << "\n"; }
输出:
list1: 0 1 3 5 9 list2: 2 4 6 7 8 merged: 0 1 2 3 4 5 6 7 8 9
参阅
  从另一 forward_list 移动元素 (公开成员函数)  |