# unordered\_set

***

**1. Tracking Unique Words in a Text**

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

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

  std::string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
  std::istringstream iss(text);

  std::string word;
  while (iss >> word) {
    uniqueWords.insert(word);
  }

  std::cout << "Unique words in the text: " << endl;
  for (const std::string& word : uniqueWords) {
    std::cout << word << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**2. Counting Occurrences of Words in a Text**

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

int main() {
  std::unordered_set<std::string> uniqueWords;
  std::unordered_map<std::string, int> wordCounts;

  std::string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
  std::istringstream iss(text);

  std::string word;
  while (iss >> word) {
    uniqueWords.insert(word);
    wordCounts[word]++;
  }

  std::cout << "Unique words in the text: " << endl;
  for (const std::string& word : uniqueWords) {
    std::cout << word << " ";
  }
  std::cout << std::endl;

  std::cout << "Word counts: " << endl;
  for (const auto& [word, count] : wordCounts) {
    std::cout << word << ": " << count << endl;
  }

  return 0;
}
```

**3. Finding Intersections of Sets**

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

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

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

  std::cout << "Intersection of sets: " << endl;
  for (const int& element : intersection) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**4. Finding Union of Sets**

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

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

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

  std::cout << "Union of sets: " << endl;
  for (const int& element : unionSet) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**5. Finding Differences of Sets**

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

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

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

  std::cout << "Difference of sets (set1 - set2): " << endl;
  for (const int& element : difference) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**6. Finding Symmetrical Differences of Sets**

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

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

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

  std::cout << "Symmetrical difference of sets: " << endl;
  for (const int& element : symmetricDifference) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**7. Checking for Set Membership**

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

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

  if (set.find(3) != set.end()) {
    std::cout << "3 is a member of the set" << std::endl;
  } else {
    std::cout << "3 is not a member of the set" << std::endl;
  }

  return 0;
}
```

**8. Removing Elements from a Set**

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

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

  set.erase(3);

  std::cout << "Set after removing 3: ";
  for (const int& element : set) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**9. Inserting Multiple Elements into a Set**

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

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

  set.insert({6, 7, 8});

  std::cout << "Set after inserting {6, 7, 8}: ";
  for (const int& element : set) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**10. Finding the Size of a Set**

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

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

  std::cout << "Size of the set: " << set.size() << std::endl;

  return 0;
}
```

**11. Combining Sets with Union**

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

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

  set1.insert(set2.begin(), set2.end());

  std::cout << "Set1 after union with set2: ";
  for (const int& element : set1) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**12. Finding Set Intersections**

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

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

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

  std::cout << "Intersection of set1 and set2: ";
  for (const int& element : intersection) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**13. Finding Set Differences**

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

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

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

  std::cout << "Difference of set1 and set2 (set1 - set2): ";
  for (const int& element : difference) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**14. Combining Sets with Union (In-Place)**

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

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

  set1.merge(set2);

  std::cout << "set1 after merging with set2: ";
  for (const int& element : set1) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**15. Finding Set Unions**

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

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

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

  std::cout << "Union of set1 and set2: ";
  for (const int& element : unionSet) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**16. Counting Elements in a Set**

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

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

  int count = set.count(3);

  std::cout << "Number of occurrences of 3 in the set: " << count << std::endl;

  return 0;
}
```

**17. Finding Bucket Count**

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

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

  int bucketCount = set.bucket_count();

  std::cout << "Number of buckets in the set: " << bucketCount << std::endl;

  return 0;
}
```

**18. Finding the Maximum Load Factor**

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

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

  float maxLoadFactor = set.max_load_factor();

  std::cout << "Maximum load factor of the set: " << maxLoadFactor << std::endl;

  return 0;
}
```

**19. Finding the Load Factor**

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

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

  float loadFactor = set.load_factor();

  std::cout << "Load factor of the set: " << loadFactor << std::endl;

  return 0;
}
```

**20. Checking if a Set is Empty**

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

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

  bool isEmpty = set.empty();

  std::cout << "Is the set empty? " << (isEmpty ? "Yes" : "No") << std::endl;

  return 0;
}
```

**21. Clearing a Set**

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

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

  set.clear();

  std::cout << "Set after clearing: ";
  for (const int& element : set) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**22. Rehashing a Set**

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

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

  set.rehash(10);

  std::cout << "Set after rehashing: ";
  for (const int& element : set) {
    std::cout << element << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**23. Using Custom Hash Function**

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

struct MyHash {
  size_t operator()(const std::string& key) const {
    return std::hash<std::string>()(key) % 10;
  }
};

int main() {
  std::unordered_set<std::string, MyHash> set = {"apple", "banana", "cherry"};

  std::cout << "Set contents: ";
  for (const std::string& fruit : set) {
    std::cout << fruit << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**24. Using Custom Key Equality Comparator**

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

struct MyComparator {
  bool operator()(const std::string& a, const std::string& b) const {
    return a.size() < b.size();
  }
};

int main() {
  std::unordered_set<std::string, std::hash<std::string>, MyComparator> set = {"apple", "banana", "cherry"};

  std::cout << "Set contents: ";
  for (const std::string& fruit : set) {
    std::cout << fruit << " ";
  }
  std::cout << std::endl;

  return 0;
}
```

**25. Finding the Bucket for an Element**

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

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

  int bucketIndex = set.bucket(3);

  std::cout << "Bucket index for the element 3: " << bucketIndex << std::endl;

  return 0;
}
```

**26. Computing the Hash Value for an Element**

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

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

  size_t hashValue = set.hash_function()(3);

  std::cout << "Hash value for the element 3: " << hashValue << std::endl;

  return 0;
}
```

**27. Iterating Over a Set**

```cpp
#include <unordered_set

```
