관리 메뉴

Kim's Programming

Algorithm - set_difference() 본문

STL - Algorithm/Algorithm - Merge

Algorithm - set_difference()

Programmer. 2017. 6. 24. 14:03

원형)


1
2
3
4
5
6
7
8
9
10
11
12
//기본형
template <class InputIterator1, class InputIterator2, class OutputIterator>
  OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2,
                                 OutputIterator result);
 
//사용자 정의형
template <class InputIterator1, class InputIterator2,
          class OutputIterator, class Compare>
  OutputIterator set_difference (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,13,15 };
    std::vector<int> vectorIntersection(16);
    std::vector<int>::iterator iter;
 
    std::sort(vector.begin(), vector.end());
    std::sort(vector2.begin(), vector2.end());
    Print(vector);
    Print(vector2);
 
    iter = std::set_difference(vector.begin(), vector.end(), vector2.begin(), vector2.end(), vectorIntersection.begin());
    vectorIntersection.resize(iter - vectorIntersection.begin());
    Print(vectorIntersection);
}
cs



리턴값)


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



결과)




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

Algorithm - set_symmetric_difference()  (0) 2017.06.24
Algorithm - set_intersection()  (0) 2017.06.24
Algorithm - set_union()  (0) 2017.06.24
Algorithm - includes()  (0) 2017.06.24
Algorithm - inplace_merge()  (0) 2017.06.24