C++ Set Algorithms: Intersection, Union, and Difference Operations

set_intersection

The set_intersection algorithm computes the intersection of two sorted ranges. It returns the end of the output range.

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

void display(int value) {
    std::cout << value << " ";
}

int main() {
    std::vector<int> first = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> second = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    
    std::vector<int> result;
    result.resize(std::min(first.size(), second.size()));
    
    auto endIterator = std::set_intersection(
        first.begin(), first.end(),
        second.begin(), second.end(),
        result.begin()
    );
    
    std::for_each(result.begin(), endIterator, display);
    std::cout << std::endl;
    
    return 0;
}
</int></int></int></algorithm></vector></iostream>

set_union

The set_union algorithm constructs a sorted range that is the union of two sorted input ranges.

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

void display(int value) {
    std::cout << value << " ";
}

int main() {
    std::vector<int> first = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> second = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    
    std::vector<int> result;
    result.resize(first.size() + second.size());
    
    auto endIterator = std::set_union(
        first.begin(), first.end(),
        second.begin(), second.end(),
        result.begin()
    );
    
    std::for_each(result.begin(), endIterator, display);
    std::cout << std::endl;
    
    return 0;
}
</int></int></int></algorithm></vector></iostream>

set_difference

The set_difference algorihtm computes the difference between two sorted ranges. The order of the input ranges affects the result.

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

void display(int value) {
    std::cout << value << " ";
}

int main() {
    std::vector<int> first = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> second = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    
    std::vector<int> result;
    result.resize(std::max(first.size(), second.size()));
    
    auto endIterator1 = std::set_difference(
        first.begin(), first.end(),
        second.begin(), second.end(),
        result.begin()
    );
    
    std::cout << "Elements in first but not in second: ";
    std::for_each(result.begin(), endIterator1, display);
    std::cout << std::endl;
    
    auto endIterator2 = std::set_difference(
        second.begin(), second.end(),
        first.begin(), first.end(),
        result.begin()
    );
    
    std::cout << "Elements in second but not in first: ";
    std::for_each(result.begin(), endIterator2, display);
    std::cout << std::endl;
    
    return 0;
}
</int></int></int></algorithm></vector></iostream>

Important: The order of the input ranges matters for set_difference. The result will contain elements that are in the first range but not in the second.

Tags: C++ STL algorithms set_operations intersection

Posted on Wed, 04 Nov 2026 16:08:54 +0000 by upnxwood16