#include <iostream>
#include <string>
#include <set>
// Custom object
struct Person {
std::string name;
int age;
// Operator to compare two Person objects
bool operator<(const Person& other) const {
return name < other.name; // Customize comparison based on name
}
};
// Custom hash function
struct PersonHash {
std::size_t operator()(const Person& person) const {
return std::hash<std::string>()(person.name); // Hash based on name
}
};
// Custom equality operator
struct PersonEqual {
bool operator()(const Person& lhs, const Person& rhs) const {
return lhs.name == rhs.name && lhs.age == rhs.age; // Check equality by name and age
}
};
int main() {
// Create a set of Person
std::set<Person> people;
people.insert({"Alice", 30});
people.insert({"Bob", 25});
// Print people
for (const auto& person : people) {
std::cout << person.name << " is " << person.age << " years old." << std::endl;
}
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?