관리 메뉴

Kim's Programming

Algorithm - set_union() 본문

STL - Algorithm/Algorithm - Merge

Algorithm - set_union()

Programmer. 2017. 6. 24. 13:19

원형)


1
2
3
4
5
6
7
8
9
10
11
12
//기본형
template <class InputIterator1, class InputIterator2, class OutputIterator>
  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, InputIterator2 last2,
                            OutputIterator result);
 
//사용자 정의형
template <class InputIterator1, class InputIterator2,
          class OutputIterator, class Compare>
  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, InputIterator2 last2,
                            OutputIterator result, Compare comp);
cs



의미)


정렬된 [Iterator first1, Iterator last1)사이의 데이터들과 정렬된 [Iterator first2, Iterator last2)사이에 있는 데이터들을 이용하여 Iterator result가 가리키는 위치부터 삽입한다. 합집합이기 떄문에 공통된 데이터에 대해서는 중복으로 삽입하지 않는다.



소스)


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
26
27
#include<iostream>
#include<algorithm>
#include<vector>
 
void Print(const std::vector<int>& target)
{
    for (std::vector<int>::const_iterator iterPos = target.cbegin(); iterPos != target.cend(); iterPos++)
        std::cout << *iterPos << " ";
    std::cout << std::endl;
}
 
void main()
{
    std::vector<int> vector = { 10,5,20,15,30 };
    std::vector<int> vector2 = { 1,3,5,7,9,11,10 };
    std::vector<int> vectorUnion(17);
    std::vector<int>::iterator iter;
 
    std::sort(vector.begin(), vector.end());
    std::sort(vector2.begin(), vector2.end());
    Print(vector);
    Print(vector2);
 
    iter = std::set_union(vector.begin(), vector.end(), vector2.begin(), vector2.end(), vectorUnion.begin());
    vectorUnion.resize(iter - vectorUnion.begin());
    Print(vectorUnion);
}
cs



리턴값)


만들어진 데이터들의 범위의 가장 마지막을 가리키는 Iterator를 리턴합니다.



결과)




'STL - Algorithm > Algorithm - Merge' 카테고리의 다른 글

Algorithm - set_difference()  (0) 2017.06.24
Algorithm - set_intersection()  (0) 2017.06.24
Algorithm - includes()  (0) 2017.06.24
Algorithm - inplace_merge()  (0) 2017.06.24
Algorithm - merge()  (0) 2017.06.24