관리 메뉴

Kim's Programming

Algorithm - unique_copy() 본문

STL - Algorithm/Algorithm - Modifying

Algorithm - unique_copy()

Programmer. 2017. 7. 13. 12:13

원형)


1
2
3
4
5
6
7
8
9
//기본형
template <class InputIterator, class OutputIterator>
  OutputIterator unique_copy (InputIterator first, InputIterator last,
                              OutputIterator result);
 
//사용자 정의형
template <class InputIterator, class OutputIterator, class BinaryPredicate>
  OutputIterator unique_copy (InputIterator first, InputIterator last,
                              OutputIterator result, BinaryPredicate pred);
cs



의미)


[Iterator first, Iterator last) 사이에 있는 데이터들 중에서 중복되는 데이터들을 제외하여 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.begin(); iterPos != target.cend(); iterPos++)
        std::cout << *iterPos << ' ';
    std::cout << std::endl;
}
 
void main()
{
    std::vector<int> vector = { 10,20,20,20,30,30,20,20,10 };
    std::vector<int> vectorCopy(9);
    std::vector<int>::iterator iter;
 
    std::cout << "Vector--->";Print(vector);
    std::cout << "VectorCopy--->";Print(vectorCopy);
 
    iter = std::unique_copy(vector.begin(), vector.end(), vectorCopy.begin());
    vectorCopy.resize(std::distance(vectorCopy.begin(), iter));
 
    std::cout << "Vector--->";Print(vector);
    std::cout << "VectorCopy--->";Print(vectorCopy);
}
 
cs



리턴값)


복사된 값들의 마지막을 가리키는 Iterator를 리턴합니다.



결과)




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

Algorithm - reverse_copy()  (0) 2017.07.13
Algorithm - reverse()  (0) 2017.07.13
Algorithm - unique()  (0) 2017.07.13
Algorithm - remove_copy_if()  (0) 2017.07.13
Algorithm - remove_copy()  (0) 2017.07.13