函式模板
<algorithm>

std::set_union

預設 (1)
template <class InputIterator1, class InputIterator2, class OutputIterator>  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,                            InputIterator2 first2, InputIterator2 last2,                            OutputIterator result);
自定義 (2)
template <class InputIterator1, class InputIterator2,          class OutputIterator, class Compare>  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,                            InputIterator2 first2, InputIterator2 last2,                            OutputIterator result, Compare comp);
兩個已排序範圍的並集
構造一個以 result 指向的位置開始的已排序範圍,該範圍是兩個已排序範圍 [first1,last1)[first2,last2) 的*集合並集*。

兩個集合的*並集*是由存在於其中一個集合或兩個集合中的元素組成的。第二個範圍中與第一個範圍中的元素等價的元素不會被複制到結果範圍。

第一個版本使用 operator< 比較元素,第二個版本使用 comp 比較元素。兩個元素 ab 被視為等價,如果 (!(a<b) && !(b<a)) 或者如果 (!comp(a,b) && !comp(b,a))

範圍中的元素應已根據相同的標準(operator<comp)排序。生成的範圍也根據此標準進行排序。

此函式模板的行為等同於
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class InputIterator1, class InputIterator2, class OutputIterator>
  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, InputIterator2 last2,
                            OutputIterator result)
{
  while (true)
  {
    if (first1==last1) return std::copy(first2,last2,result);
    if (first2==last2) return std::copy(first1,last1,result);

    if (*first1<*first2) { *result = *first1; ++first1; }
    else if (*first2<*first1) { *result = *first2; ++first2; }
    else { *result = *first1; ++first1; ++first2; }
    ++result;
  }
}

引數

first1, last1
輸入迭代器指向第一個已排序序列的初始位置和結束位置。使用的範圍是 [first1,last1),它包含 first1last1 之間的所有元素,包括 first1 指向的元素,但不包括 last1 指向的元素。
first2, last2
輸入迭代器,指向第二個已排序序列的起始和結束位置。使用的範圍是 [first2,last2)
result
輸出迭代器,指向儲存結果序列的範圍的起始位置。
被指向的型別應支援被賦值為來自其他範圍的元素值。
comp
二元函式,它接受由輸入迭代器指向的兩種型別的引數,並返回一個可轉換為 bool 的值。返回的值表示第一個引數是否在它定義的特定*嚴格弱序*中被認為排在第二個之前。
該函式不得修改其任何引數。
這可以是一個函式指標,也可以是一個函式物件。

這些範圍不應重疊。

返回值

指向所構造範圍結束位置的迭代器。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// set_union example
#include <iostream>     // std::cout
#include <algorithm>    // std::set_union, std::sort
#include <vector>       // std::vector

int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  std::vector<int> v(10);                      // 0  0  0  0  0  0  0  0  0  0
  std::vector<int>::iterator it;

  std::sort (first,first+5);     //  5 10 15 20 25
  std::sort (second,second+5);   // 10 20 30 40 50

  it=std::set_union (first, first+5, second, second+5, v.begin());
                                               // 5 10 15 20 25 30 40 50  0  0
  v.resize(it-v.begin());                      // 5 10 15 20 25 30 40 50

  std::cout << "The union has " << (v.size()) << " elements:\n";
  for (it=v.begin(); it!=v.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

輸出
The union has 8 elements:
 5 10 15 20 25 30 40 50


複雜度

線性時間,最多為 2*(count1+count2)-1(其中 countXfirstXlastX 之間的距離):比較和賦值元素。

資料競爭

訪問範圍 [first1,last1)[first2,last2) 中的物件。
修改 result 和返回的迭代器之間的範圍內的物件。

異常

如果任何元素比較、元素賦值或迭代器操作丟擲異常,則丟擲異常。
請注意,無效引數會導致未定義行為

另見