首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::forward_list::merge

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比较。

二次

代码语言:javascript
复制
#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";
}

二次

产出:

二次

代码语言:javascript
复制
list1:   0 1 3 5 9
list2:   2 4 6 7 8
merged:  0 1 2 3 4 5 6 7 8 9

二次

另见

splice_after

moves elements from another forward_list (public member function)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com