المتغيرات
فضاءات التسمية
أفعال

std::{{{1}}}::merge

من cppreference.com
void merge( {{{1}}}& other );
(1) قالب:mark since none
void merge( {{{1}}}&& other );
(1) (since C++11)
template <class Compare>
void merge( {{{1}}}& other, Compare comp );
(2) قالب:mark since none
template <class Compare>
void merge( {{{1}}}&& other, Compare comp );
(2) (since C++11)

Merges two sorted lists into one. The lists should be sorted into ascending order.

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‎.

This operation is stable: for equivalent elements in the two lists, the elements from ‎*this‎ shall always precede the elements from ‎other‎, and the order of equivalent elements of ‎*this‎ and ‎other‎ does not change.

محتويات

[تعديل] Parameters

other - another container to merge

قالب:par cmp ord

[تعديل] Return value

(none)

[تعديل] Example

#include <iostream>
#include <{{{1}}}>
 
std::ostream& operator<<(std::ostream& ostr, const std::{{{1}}}<int>& list)
{
    for (auto &i : list) {
        ostr << " " << i;
    }
    return ostr;
}
 
int main()
{
    std::{{{1}}}<int> list1 = { 5,9,0,1,3 };
    std::{{{1}}}<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

[تعديل] Complexity

at most size() + other.size() - 1 comparisons.

[تعديل] See also