# flat\_set

***

**1. Storing Unique Elements in a Container:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<int> numbers;
  numbers.insert(1);
  numbers.insert(2);
  numbers.insert(3);
  numbers.insert(1); // Duplicates are ignored

  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1 2 3
  }
  return 0;
}
```

**2. Removing Duplicates from an Existing Container:**

```cpp
#include <unordered_set>
#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers = {1, 2, 3, 1, 2, 4};

  std::unordered_set<int> unique_numbers(numbers.begin(), numbers.end());

  std::cout << "Unique numbers: ";
  for (int n : unique_numbers) {
    std::cout << n << " "; // Outputs: 1 2 3 4
  }
  std::cout << "\n";

  return 0;
}
```

**3. Finding Common Elements in Multiple Containers:**

```cpp
#include <unordered_set>
#include <vector>
#include <iostream>

int main() {
  std::vector<int> set1 = {1, 2, 3};
  std::vector<int> set2 = {2, 3, 4};

  std::unordered_set<int> intersection;

  // Insert elements from set1 into intersection
  for (int n : set1) {
    intersection.insert(n);
  }

  // Iterate through set2 and find intersections
  for (int n : set2) {
    if (intersection.count(n) > 0) {
      std::cout << n << " is common to both sets.\n";
    }
  }

  return 0;
}
```

**4. Checking for Set Membership:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<std::string> names = {"Alice", "Bob", "Carol"};

  std::cout << "Is Alice in the set? " << (names.count("Alice") > 0) << "\n"; // Outputs: true

  std::cout << "Is Eve in the set? " << (names.count("Eve") > 0) << "\n"; // Outputs: false
  return 0;
}
```

**5. Combining Multiple Sets:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {4, 5, 6};

  std::unordered_set<int> combined_set(set1); // Copy constructor initializes from set1
  combined_set.insert(set2.begin(), set2.end()); // Inserts elements from set2

  std::cout << "Combined set: ";
  for (int n : combined_set) {
    std::cout << n << " "; // Outputs: 1 2 3 4 5 6
  }
  std::cout << "\n";

  return 0;
}
```

**6. Intersection and Union of Sets:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {2, 4, 5};

  std::unordered_set<int> intersection;
  std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(),
                        std::inserter(intersection, intersection.begin()));

  std::unordered_set<int> union_set;
  std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(),
                  std::inserter(union_set, union_set.begin()));

  std::cout << "Intersection: ";
  for (int n : intersection) {
    std::cout << n << " "; // Outputs: 2
  }
  std::cout << "\n";

  std::cout << "Union: ";
  for (int n : union_set) {
    std::cout << n << " "; // Outputs: 1 2 3 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**7. Counting Unique Elements:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<std::string> colors = {"Red", "Orange", "Yellow", "Green",
                                            "Blue", "Indigo", "Violet"};

  std::cout << "Number of unique colors: " << colors.size() << "\n"; // Outputs: 7
  return 0;
}
```

**8. Testing for Subset and Superset:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<int> subset = {1, 2, 3};
  std::unordered_set<int> superset = {1, 2, 3, 4, 5};

  std::cout << "Is subset a subset of superset? "
            << (std::includes(subset.begin(), subset.end(), superset.begin(),
                              superset.end())
                   ? "Yes"
                   : "No")
            << "\n"; // Outputs: Yes

  std::cout << "Is superset a subset of subset? "
            << (std::includes(superset.begin(), superset.end(), subset.begin(),
                              subset.end())
                   ? "Yes"
                   : "No")
            << "\n"; // Outputs: No
  return 0;
}
```

**9. Equality Testing:**

```cpp
#include <unordered_set>
#include <iostream>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {1, 2, 3};

  std::cout << "Are set1 and set2 equal? "
            << (set1 == set2 ? "Yes" : "No") << "\n"; // Outputs: Yes
  return 0;
}
```

**10. Storing Custom Objects:**

```cpp
#include <unordered_set>
#include <string>

struct Person {
  std::string name;
  int age;
};

struct PersonHasher {
  std::size_t operator()(const Person& p) const {
    return std::hash<std::string>()(p.name) ^ std::hash<int>()(p.age);
  }
};

struct PersonComparator {
  bool operator()(const Person& p1, const Person& p2) const {
    return p1.name == p2.name && p1.age == p2.age;
  }
};

int main() {
  std::unordered_set<Person, PersonHasher, PersonComparator> people;
  Person alice{"Alice", 25};
  people.insert(alice);
  Person bob{"Bob", 30};
  people.insert(bob);

  std::cout << "People in the set: ";
  for (const auto& p : people) {
    std::cout << p.name << ", " << p.age << "; "; // Outputs: Alice, 25; Bob, 30;
  }
  std::cout << "\n";

  return 0;
}
```

**11. Customizing Hash Function:**

```cpp
#include <unordered_set>
#include <string>

struct MyHasher {
  std::size_t operator()(const std::string& str) const {
    std::size_t hash_value = 0;
    for (char c : str) {
      hash_value = 31 * hash_value + c;
    }
    return hash_value;
  }
};

int main() {
  std::unordered_set<std::string, MyHasher> strings;
  strings.insert("Hello");
  strings.insert("World");

  std::cout << "Strings in the set: ";
  for (const auto& str : strings) {
    std::cout << str << " "; // Outputs: Hello World
  }
  std::cout << "\n";

  return 0;
}
```

**12. Customizing Comparison Function:**

```cpp
#include <unordered_set>
#include <string>

struct MyComparator {
  bool operator()(const std::string& str1, const std::string& str2) const {
    return str1.length() < str2.length();
  }
};

int main() {
  std::unordered_set<std::string, std::hash<std::string>, MyComparator> strings;
  strings.insert("Hello");
  strings.insert("World");
  strings.insert("!");

  std::cout << "Strings in the set, sorted by length: ";
  for (const auto& str : strings) {
    std::cout << str << " "; // Outputs: ! World Hello
  }
  std::cout << "\n";

  return 0;
}
```

**13. Using Flat Sets:**

```cpp
#include <unordered_flat_set>
#include <string>

int main() {
  std::unordered_flat_set<std::string> strings;
  strings.insert("Hello");
  strings.insert("World");

  std::cout << "Strings in the flat set: ";
  for (const auto& str : strings) {
    std::cout << str << " "; // Outputs: Hello World
  }
  std::cout << "\n";

  return 0;
}
```

**14. Checking for Containment:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Does the set contain 3? " << (numbers.find(3) != numbers.end())
            << "\n"; // Outputs: true
  std::cout << "Does the set contain 6? " << (numbers.find(6) != numbers.end())
            << "\n"; // Outputs: false
  return 0;
}
```

**15. Erasing Elements:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  numbers.erase(3);

  std::cout << "Set after erasing: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1 2 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**16. Moving Elements:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2;

  set2 = std::move(set1);

  std::cout << "Set1 after move: ";
  for (int n : set1) {
    std::cout << n << " "; // Outputs: empty
  }
  std::cout << "\n";

  std::cout << "Set2 after move: ";
  for (int n : set2) {
    std::cout << n << " "; // Outputs: 1 2 3
  }
  std::cout << "\n";

  return 0;
}
```

**17. Swapping Elements:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {4, 5, 6};

  set1.swap(set2);

  std::cout << "Set1 after swap: ";
  for (int n : set1) {
    std::cout << n << " "; // Outputs: 4 5 6
  }
  std::cout << "\n";

  std::cout << "Set2 after swap: ";
  for (int n : set2) {
    std::cout << n << " "; // Outputs: 1 2 3
  }
  std::cout << "\n";

  return 0;
}
```

**18. Finding the Bucket Count:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Bucket count: " << numbers.bucket_count() << "\n"; // Output: 4
  return 0;
}
```

**19. Calculating the Load Factor:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Load factor: " << numbers.load_factor() << "\n"; // Output: 0.125
  return 0;
}
```

**20. Resizing the Set:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  numbers.reserve(10);

  std::cout << "Capacity after resize: " << numbers.capacity() << "\n"; // Output: 13
  return 0;
}
```

**21. Clearing the Set:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  numbers.clear();

  std::cout << "Set after clear: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: empty
  }
  std::cout << "\n";

  return 0;
}
```

**22. Counting the Elements:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Number of elements: " << numbers.size() << "\n"; // Output: 5
  return 0;
}
```

**23. Checking for Emptiness:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers;

  std::cout << "Is the set empty? " << numbers.empty() << "\n"; // Outputs: true

  numbers.insert(1);

  std::cout << "Is the set empty? " << numbers.empty() << "\n"; // Outputs: false
  return 0;
}
```

**24. Inserting a Single Element:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers;

  numbers.insert(1);

  std::cout << "Set after insertion: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1
  }
  std::cout << "\n";

  return 0;
}
```

**25. Inserting a Range of Elements:**

```cpp
#include <unordered_set>
#include <vector>

int main() {
  std::unordered_set<int> numbers;

  std::vector<int> values = {1, 2, 3, 4, 5};

  numbers.insert(values.begin(), values.end());

  std::cout << "Set after insertion: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1 2 3 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**26. Inserting an Initializer List:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Set after initialization: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1 2 3 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**27. Removing a Single Element:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  numbers.erase(3);

  std::cout << "Set after removal: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1 2 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**28. Removing a Range of Elements:**

```cpp
#include <unordered_set>
#include <vector>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::vector<int> values = {2, 4};

  numbers.erase(values.begin(), values.end());

  std::cout << "Set after removal: ";
  for (int n : numbers) {
    std::cout << n << " "; // Outputs: 1 3 5
  }
  std::cout << "\n";

  return 0;
}
```

**29. Finding the Maximum Element:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Maximum element: " << *numbers.max_element() << "\n"; // Outputs: 5
  return 0;
}
```

**30. Finding the Minimum Element:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::cout << "Minimum element: " << *numbers.min_element() << "\n"; // Outputs: 1
  return 0;
}
```

**31. Using the Equality Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {1, 2, 3};

  std::cout << "Are the sets equal? " << (set1 == set2) << "\n"; // Outputs: true
  return 0;
}
```

**32. Using the Inequality Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {1, 2, 4};

  std::cout << "Are the sets unequal? " << (set1 != set2) << "\n"; // Outputs: true
  return 0;
}
```

**33. Using the Subset Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> subset = {1, 2};
  std::unordered_set<int> superset = {1, 2, 3};

  std::cout << "Is subset a subset of superset? " << (subset.subset_of(superset))
            << "\n"; // Outputs: true
  return 0;
}
```

**34. Using the Proper Subset Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> subset = {1, 2};
  std::unordered_set<int> superset = {1, 2, 3};

  std::cout << "Is subset a proper subset of superset? "
            << (subset.proper_subset_of(superset)) << "\n"; // Outputs: true
  return 0;
}
```

**35. Using the Superset Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> subset = {1, 2};
  std::unordered_set<int> superset = {1, 2, 3};

  std::cout << "Is superset a superset of subset? "
            << (superset.superset_of(subset)) << "\n"; // Outputs: true
  return 0;
}
```

**36. Using the Proper Superset Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> subset = {1, 2};
  std::unordered_set<int> superset = {1, 2, 3};

  std::cout << "Is superset a proper superset of subset? "
            << (superset.proper_superset_of(subset)) << "\n"; // Outputs: true
  return 0;
}
```

**37. Using the Disjoint Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2};
  std::unordered_set<int> set2 = {3, 4};

  std::cout << "Are the sets disjoint? " << (set1.is_disjoint(set2)) << "\n";
        // Outputs: true
  return 0;
}
```

**38. Using the Equal Range Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> numbers = {1, 2, 3, 4, 5};

  std::pair<std::unordered_set<int>::iterator, std::unordered_set<int>::iterator> range
    = numbers.equal_range(3);

  std::cout << "Equal range for 3: ";
  for (auto it = range.first; it != range.second; ++it) {
    std::cout << *it << " "; // Outputs: 3
  }
  std::cout << "\n";

  return 0;
}
```

**39. Using the Set Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {3, 4, 5};

  std::unordered_set<int> intersection;
  std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(),
                        std::inserter(intersection, intersection.begin()));

  std::cout << "Intersection: ";
  for (int n : intersection) {
    std::cout << n << " "; // Outputs: 3
  }
  std::cout << "\n";

  return 0;
}
```

**40. Using the Multiset Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {3, 4, 5};

  std::unordered_set<int> multiset;
  std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(),
                  std::inserter(multiset, multiset.begin()));

  std::cout << "Multiset: ";
  for (int n : multiset) {
    std::cout << n << " "; // Outputs: 1 2 3 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**41. Using the Difference Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {3, 4, 5};

  std::unordered_set<int> difference;
  std::set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(),
                      std::inserter(difference, difference.begin()));

  std::cout << "Difference: ";
  for (int n : difference) {
    std::cout << n << " "; // Outputs: 1 2
  }
  std::cout << "\n";

  return 0;
}
```

**42. Using the Symmetric Difference Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {3, 4, 5};

  std::unordered_set<int> symmetric_difference;
  std::set_symmetric_difference(set1.begin(), set1.end(), set2.begin(), set2.end(),
                                std::inserter(symmetric_difference,
                                              symmetric_difference.begin()));

  std::cout << "Symmetric difference: ";
  for (int n : symmetric_difference) {
    std::cout << n << " "; // Outputs: 1 2 4 5
  }
  std::cout << "\n";

  return 0;
}
```

**43. Using the Swap Operator:**

```cpp
#include <unordered_set>

int main() {
  std::unordered_set<int> set1 = {1, 2, 3};
  std::unordered_set<int> set2 = {4, 5, 6};

  set1.swap(set2);

  std::cout << "Set1 after swap: ";
  for (int n : set1) {
    std::cout << n << " "; // Outputs: 4 5 6
  }
  std::cout << "\n";

  std::cout << "Set2 after swap: ";
  for (int n : set2) {
    std::cout << n << " "; // Outputs: 1 2 3
  }
  std::cout << "\n";

  return 0;
}
```

**44. Using the Hash Function:**

```cpp
#include <unordered_set>
#include <functional>

int main() {
  std::unordered_set<int> numbers;

  std::hash<int> hash_function;

  std::cout << "Hash value for 1: " << hash_function(1) << "\n"; // Output: 1
  return 0;
}
```

**45. Customizing the Hash Function:**

```cpp
#include <unordered_set>
#include <functional>
#include <string>

struct MyHasher {
  std::size_t operator()(const std::string& str) const {
    std::size_t hash_value = 0;
    for (char c : str) {
      hash_value = 31 * hash_value + c;
    }
    return hash_value;
  }
};

int main() {
  std::unordered_set<std::string, MyHasher> strings;

  strings.insert("Hello");
  strings.insert("World");

  std::cout << "Hash value for Hello: " << MyHasher()("Hello") << "\n"; // Output: 35475622
  return 0;
}
```

**46. Customizing the Comparison Function:**

```cpp
#include <unordered_set>
#include <functional>
#include <string>

struct MyComparator {
  bool operator()(const std::string& str1, const std::string& str2) const {
    return str1.length() < str2.length();
  }
};

int main() {
  std::unordered_set<std::string, std::hash<std::string>, MyComparator> strings;

  strings.insert("Hello");
  strings.insert("World");

  std::cout << "Comparison result: "
            << (MyComparator()("Hello", "World") ? "true" : "false")
            << "\n"; // Output: true
  return 0;
}
```

**47. Using the Key Extractor:**

```cpp
#include <unordered_set>
#include <functional>
#include <string>

struct Person {
  std::string name;
  int age;
};

struct PersonExtractor {
  std::string operator()(const Person& person) const {
    return person.name;
  }
};

int main() {
  std::unordered_set<Person, std::hash<std::string>, PersonExtractor> people;

  Person alice = {"Alice", 25};
  people.insert(alice);

  std::cout << "Key extracted from Alice: " << PersonExtractor()(alice) << "\n";
        // Output: Alice
  return 0;
}
```

**48. Using the Allocator:**

```cpp
#include <unordered_set>
#include <memory>

int main() {
  std::unordered_set<int, std::hash<int>, std::equal_to<int>,
                      std::allocator<int>> numbers;

  std::allocator<int> allocator;

  int* p = allocator.allocate(1);

  *p = 10;

  numbers.insert(p);

  std::cout << "Value inserted into the set: " << *p << "\n"; // Output: 10
  return 0;
}
```

**49. Using the Debug Allocator:**

```cpp
#include <unordered_set>
#include <memory>

int main() {
  std::unordered_set<int, std::hash<int>, std::equal_to<int>,
                      std::allocator<int>> numbers;

  std::allocator<int> allocator(
      std::allocator_traits<std::allocator<int>>::select_on_container_copy_construction,
      &std::allocator<int>());

  int* p = allocator.allocate(1);

  *p = 10;

  numbers.insert(p);

  std::cout << "Value inserted into the set: " << *p << "\n"; // Output: 10
  return 0;
}
```

**50. Using emplace:**

```cpp
#include <unordered_set>
#include <string>

int main() {
  std::unordered_set<std::string> strings;

  strings.emplace("Hello");
  strings.emplace("World");

  std::cout << "Strings in the set: ";
  for (const auto& str : strings) {
    std::cout << str << " "; // Outputs: Hello World
  }
  std::cout << "\n";

  return 0;
}
```
