std::list::merge
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
void merge( list& other ); |
(1) | |
void merge( list&& other ); |
(1) | (C++11 起) |
template <class Compare> void merge( list& other, Compare comp ); |
(2) | |
template <class Compare> void merge( list&& other, Compare comp ); |
(2) | (C++11 起) |
合并两个已排序的列表为一体。应该按升序排列的列表.
原文:
Merges two sorted lists into one. The lists should be sorted into ascending order.
没有这样的元素复制。在手术后变成空容器
other
。如果this == &other函数什么也不做。如果get_allocator() != other.get_allocator(),该行为是未定义的。没有迭代器或引用变得无效,不进*this,除了移动的元素的迭代器现在到other
。 operator<的第一个版本使用比较的元素,第二个版本使用给定的比较函数comp
.原文:
No elements are copied. The container
other
becomes empty after the operation. The function does nothing if this == &other. If get_allocator() != other.get_allocator(), the behavior is undefined. No iterators or references become invalidated, except that the iterators of moved elements now refer into *this, not into other
. The first version uses operator< to compare the elements, the second version uses the given comparison function comp
.目录 |
[编辑] 参数
other | - | 另一个容器合并
|
comp | - | comparison function which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. |
[编辑] 返回值
(无)
[编辑] 为例
#include <iostream> #include <list> std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) { for (auto &i : list) { ostr << " " << i; } return ostr; } int main() { std::list<int> list1 = { 5,9,0,1,3 }; std::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
[编辑] 复杂性
在最size() + other.size() - 1比较.
原文:
at most size() + other.size() - 1 comparisons.
[编辑] 另请参阅
从另一个 list 移动元素 (公共成员函数) |