Movatterモバイル変換


[0]ホーム

URL:


cplusplus.com

Reference

<algorithm>

function template
<algorithm>

std::set_union

default (1)
template <class InputIterator1, class InputIterator2, class OutputIterator>  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,                            InputIterator2 first2, InputIterator2 last2,                            OutputIterator result);
custom (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);
Union of two sorted ranges
Constructs a sorted range beginning in the location pointed byresult with theset union of the two sorted ranges[first1,last1) and[first2,last2).

Theunion of two sets is formed by the elements that are present in either one of the sets, or in both. Elements from the second range that have an equivalent element in the first range are not copied to the resulting range.

The elements are compared usingoperator< for the first version, andcomp for the second. Two elements,a andb are considered equivalent if(!(a<b) && !(b<a)) or if(!comp(a,b) && !comp(b,a)).

The elements in the ranges shall already be ordered according to this same criterion (operator< orcomp). The resulting range is also sorted according to this.

The behavior of this function template is equivalent to:
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; }elseif (*first2<*first1) { *result = *first2; ++first2; }else { *result = *first1; ++first1; ++first2; }    ++result;  }}

Parameters

first1, last1
Input iterators to the initial and final positions of the first sorted sequence. The range used is[first1,last1), which contains all the elements betweenfirst1 andlast1, including the element pointed byfirst1 but not the element pointed bylast1.
first2, last2
Input iterators to the initial and final positions of the second sorted sequence. The range used is[first2,last2).
result
Output iterator to the initial position of the range where the resulting sequence is stored.
The pointed type shall support being assigned the value of an element from the other ranges.
comp
Binary function that accepts two arguments of the types pointed by the input iterators, and returns a value convertible tobool. The value returned indicates whether the first argument is considered to go before the second in the specificstrict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

The ranges shall not overlap.

Return value

An iterator to the end of the constructed range.

Example

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::vectorint 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;}

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


Complexity

Up to linear in2*(count1+count2)-1 (wherecountX is thedistance betweenfirstX andlastX): Compares and assigns elements.

Data races

The objects in the ranges[first1,last1) and[first2,last2)are accessed.
The objects in the range betweenresult and the returned value are modified.

Exceptions

Throws if any of the element comparisons, the element assignments or the operations on iterators throws.
Note that invalid arguments causeundefined behavior.

See also

set_intersection
Intersection of two sorted ranges(function template)
set_difference
Difference of two sorted ranges(function template)
set_symmetric_difference
Symmetric difference of two sorted ranges(function template)
merge
Merge sorted ranges(function template)
Home page |Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved -v3.3.4s
Spotted an error? contact us

[8]ページ先頭

©2009-2026 Movatter.jp