std::list<T,Allocator>:: merge
|
void
merge
(
list
&
other
)
;
|
(1) | (自 C++26 起为 constexpr) |
|
void
merge
(
list
&&
other
)
;
|
(2) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
|
template
<
class
Compare
>
void merge ( list & other, Compare comp ) ; |
(3) | (自 C++26 起为 constexpr) |
|
template
<
class
Compare
>
void merge ( list && other, Compare comp ) ; |
(4) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
将两个已排序列表合并为一个有序列表。
- 如果 other 与 * this 指向同一对象,则不执行任何操作。
- 否则,将 other 中的所有元素转移到 * this 。合并后 other 将为空。
此操作是稳定的:
- 对于两个列表中相等的元素,来自 * this 的元素始终位于来自 other 的元素之前。
- * this 和 other 中相等元素的顺序不会改变。
- * this 或 other 未相对于比较器 comp 有序 。
- get_allocator ( ) == other. get_allocator ( ) 为 false 。
不会使任何迭代器或引用失效。指向从 * this 移出元素的指针和引用,以及指向这些元素的迭代器,将仍然指向 * this 中的相同元素,而非指向 other 。
目录参数
返回值(无) 异常如果因任何原因抛出异常,这些函数将不产生任何效果( 强异常安全保证 )。除非异常来自比较操作。 复杂度如果 other 与 * this 指向同一对象,则不执行任何比较操作。 否则,给定 N 1 为 std:: distance ( begin ( ) , end ( ) ) 且 N 2 为 std:: distance ( other. begin ( ) , other. end ( ) ) :
1,2)
最多使用
N
1
+N
2
-1
次比较,通过
operator
<
实现。
3,4)
最多
N
1
+N
2
-1
次比较函数
comp
的应用。
示例
运行此代码
#include <iostream> #include <list> std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) { for (const int i : list) ostr << ' ' << i; return ostr; } int main() { std::list<int> list1 = {5, 9, 1, 3, 3}; std::list<int> list2 = {8, 7, 2, 3, 4, 4}; list1.sort(); list2.sort(); std::cout << "list1: " << list1 << '\n'; std::cout << "list2: " << list2 << '\n'; list1.merge(list2); std::cout << "merged:" << list1 << '\n'; } 输出: list1: 1 3 3 5 9 list2: 2 3 4 4 7 8 merged: 1 2 3 3 3 4 4 5 7 8 9 缺陷报告以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
参见
| ||||||||||||||||||||||||||||||||||||||||||||