💡 퀵 접속: cpp.kr/set_difference

set_difference

C++ 표준 라이브러리의 알고리즘으로, 두 정렬된 범위의 차집합을 계산합니다. set_difference는 원본 범위를 수정하지 않고, 첫 번째 범위에만 있고 두 번째 범위에는 없는 요소들을 다른 범위에 복사합니다. 결과는 정렬된 상태로 저장됩니다.

기본 사용법

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> set1 = {1, 2, 3, 4, 5};
    std::vector<int> set2 = {3, 4, 5, 6, 7};
    std::vector<int> result;
    
    std::cout << "첫 번째 집합: ";
    for (int n : set1) std::cout << n << " ";
    std::cout << std::endl;
    
    std::cout << "두 번째 집합: ";
    for (int n : set2) std::cout << n << " ";
    std::cout << std::endl;
    
    // 차집합 계산
    std::set_difference(set1.begin(), set1.end(),
                       set2.begin(), set2.end(),
                       std::back_inserter(result));
    
    std::cout << "차집합 (set1 - set2): ";
    for (int n : result) std::cout << n << " ";
    std::cout << std::endl;
    
    return 0;
}

실행 결과:

첫 번째 집합: 1 2 3 4 5
두 번째 집합: 3 4 5 6 7
차집합 (set1 - set2): 1 2

문자열에서 사용

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string str1 = "abcdef";
    std::string str2 = "cdefgh";
    std::string result;
    
    std::cout << "첫 번째 문자열: " << str1 << std::endl;
    std::cout << "두 번째 문자열: " << str2 << std::endl;
    
    // 차집합 계산
    std::set_difference(str1.begin(), str1.end(),
                       str2.begin(), str2.end(),
                       std::back_inserter(result));
    
    std::cout << "차집합 (str1 - str2): " << result << std::endl;
    
    return 0;
}

실행 결과:

첫 번째 문자열: abcdef
두 번째 문자열: cdefgh
차집합 (str1 - str2): ab

사용자 정의 타입에서 사용

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

class Person {
    std::string name;
    int age;
public:
    Person(const std::string& n, int a) : name(n), age(a) {}
    
    bool operator<(const Person& other) const {
        return name < other.name;
    }
    
    void print() const {
        std::cout << name << " (" << age << "세)" << std::endl;
    }
};

int main() {
    std::vector<Person> set1 = {
        Person("Alice", 20),
        Person("Bob", 30),
        Person("Charlie", 25),
        Person("David", 35)
    };
    
    std::vector<Person> set2 = {
        Person("Bob", 30),
        Person("Charlie", 25),
        Person("Eve", 22),
        Person("Frank", 40)
    };
    
    std::vector<Person> result;
    
    std::cout << "첫 번째 집합:" << std::endl;
    for (const auto& person : set1) {
        person.print();
    }
    
    std::cout << "\n두 번째 집합:" << std::endl;
    for (const auto& person : set2) {
        person.print();
    }
    
    // 차집합 계산
    std::set_difference(set1.begin(), set1.end(),
                       set2.begin(), set2.end(),
                       std::back_inserter(result));
    
    std::cout << "\n차집합 (set1 - set2):" << std::endl;
    for (const auto& person : result) {
        person.print();
    }
    
    return 0;
}

실행 결과:

첫 번째 집합:
Alice (20세)
Bob (30세)
Charlie (25세)
David (35세)

두 번째 집합:
Bob (30세)
Charlie (25세)
Eve (22세)
Frank (40세)

차집합 (set1 - set2):
Alice (20세)
David (35세)

참고사항

  • set_difference는 algorithm 헤더에 정의되어 있습니다.
  • 원본 범위를 수정하지 않습니다.
  • 시간 복잡도는 O(n + m)입니다. 여기서 n과 m은 각각 두 범위의 크기입니다.
  • 입력 범위는 정렬되어 있어야 합니다.
  • 결과는 정렬된 상태로 저장됩니다.
  • 대상 범위는 충분한 공간이 있어야 합니다.
  • back_inserter를 사용하면 대상 컨테이너가 자동으로 확장됩니다.
  • 사용자 정의 타입을 사용할 때는 operator<가 정의되어 있어야 합니다.
  • set_intersection은 두 범위의 교집합을 계산하는 버전입니다.
  • set_union은 두 범위의 합집합을 계산하는 버전입니다.
  • set_symmetric_difference는 두 범위의 대칭차를 계산하는 버전입니다.
함수 설명
set_difference(first1, last1, first2, last2, result) 정렬된 범위 [first1, last1)와 [first2, last2)의 차집합을 result부터 시작하는 범위에 복사