在线文档教程
C++
容器 | Containers

std::forward_list::merge

STD:向前[医]清单::合并

void merge( forward_list& other (1)(since C++11)
void merge( forward_list&& other (1)(since C++11)
template <class Compare> void merge( forward_list& other, Compare comp (2)(since C++11)
template <class Compare> void merge( forward_list&& other, Compare comp (2)(since C++11)

将两个排序的列表合并为一个。列表应按升序排序。

没有复制任何元素。集装箱other手术后变为空。如果this == &other.如果get_allocator() != other.get_allocator(),该行为是未定义的。没有迭代器或引用失效,只是移动元素的迭代器现在引用到*this,而不是进入other第一个版本使用operator<为了比较元素,第二个版本使用给定的比较函数。comp...

此操作是稳定的:对于两个列表中的等效元素,*this总是在元素之前other的等价元素的顺序*thisother不会改变。

参数

other-another container to merge
comp-comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ​true if the first argument is less than (i.e. is ordered before) 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 object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type forward_list<T,Allocator>::const_iterator can be dereferenced and then implicitly converted to both of them. ​

返回值

%280%29

例外

如果抛出异常,则此函数不具有%28强异常保证%29的效果,除非异常来自比较函数。

复杂性

顶多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

二次

另见

splice_aftermoves elements from another forward_list (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/container/Forward[医]列表/合并