#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <string>
// Custom class for demonstration
class Person {
public:
std::string name;
int age;
Person(std::string n, int a) : name(n), age(a) {}
bool operator<(const Person& other) const {
return name < other.name || (name == other.name && age < other.age);
}
};
// Function to print a set of integers
void printSet(const std::set<int>& s) {
for (const auto& elem : s) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
// Function to print a set of Person objects
void printPersonSet(const std::set<Person>& s) {
for (const auto& person : s) {
std::cout << "Name: " << person.name << ", Age: " << person.age << std::endl;
}
}
int main() {
// Basic set operations
std::set<int> mySet = {10, 20, 30, 40, 50};
mySet.insert(60);
mySet.erase(30);
std::cout << "Set after operations: ";
printSet(mySet);
// Union and Intersection
std::set<int> anotherSet = {30, 40, 50, 60, 70};
std::set<int> unionSet, intersectionSet;
std::set_union(mySet.begin(), mySet.end(), anotherSet.begin(), anotherSet.end(), std::inserter(unionSet, unionSet.begin()));
std::set_intersection(mySet.begin(), mySet.end(), anotherSet.begin(), anotherSet.end(), std::inserter(intersectionSet, intersectionSet.begin()));
std::cout << "Union: ";
printSet(unionSet);
std::cout << "Intersection: ";
printSet(intersectionSet);
// Custom objects in set
std::set<Person> personSet;
personSet.insert(Person("Alice", 30));
personSet.insert(Person("Bob", 25));
personSet.insert(Person("Charlie", 35));
personSet.insert(Person("Alice", 28));
std::cout << "Person set: " << std::endl;
printPersonSet(personSet);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: