Problem11162--STL 函数使用 —— merge

11162: STL 函数使用 —— merge

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

Description

【介绍】

【头文件】

Standard Template Library: Algorithms
#include<algorithm>

【作用】

C++ offers in its STL library a merge() which is quite useful to merge sort two containers into a single container. It is defined in header “algorithm“.
Syntax 1 : Using operator “<”
Template :
template 
  outiter merge (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2,
                        outiter res)

Parameters :
beg1 :  Input iterator to initial position of first sequence.
end1 :  Input iterator to final position of first sequence.

beg2 :  Input iterator to initial position of second sequence.
end2 :  Input iterator to final position of second sequence.

res : Output Iterator to initial position of resultant container.
Return value : 
Iterator to last element of the resulting container.
// using merge() to merge the initial containers
merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin());
Syntax 2 : Using comparator function
Template :
template 
  outiter merge (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2,
                        outiter res, Compare comp)

Parameters :
beg1 :  Input iterator to initial position of first sequence.
end1 :  Input iterator to final position of first sequence.

beg2 :  Input iterator to initial position of second sequence.
end2 :  Input iterator to final position of second sequence.

res : Output Iterator to initial position of resultant container.
comp : The comparator function that returns a boolean
true/false of the each elements compared. This function 
accepts two arguments. This can be function pointer or 
function object and cannot change values.
Return value : 
Iterator to last element of the resulting container.
// comparator function to reverse merge sort
struct greaters {
    bool operator()(const long& a, const long& b) const
    {
        return a > b;
    }
};


    // using merge() to merge the initial containers
    // returns descended merged container
    merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin(), greaters());

【任务】

我们通过本题,掌握 merge() 函数使用方法。
给一个长度为 $n\ (1≤n≤10^5)$ 的整数数列,和一个长度为 $m\ (1 \leq m \leq 10^5)$,请按照要求合并成为一个序列。

Input

第一行一个整数 $n$。
第二行包括 $n$ 个整数。
第三行一个整数 $m$。
第四行包括 $m$ 个整数。
第五行表示排序方法。

Output

一行一共 $n+m$ 个整数,按照要求输出。

Sample 1 Input

5
1 4 6 3 2
5
6 2 5 7 1
<

Sample 1 Output

1 1 2 2 3 4 5 6 6 7

Sample 2 Input

5
1 4 6 3 2
5
6 2 5 7 1
>

Sample 2 Output

7 6 6 5 4 3 2 2 1 1

Source/Category