In the world of programming, managing collections of data is a fundamental task. The C++ Standard Template Library (STL) provides several containers to help with this, including set and multiset. These containers are particularly useful when you need to store unique sorted elements or handle duplicates efficiently.
A set is an associative container that stores unique elements following a specific order. By default, the elements in a set are stored in ascending order based on their values. This makes sets ideal for scenarios where you need to ensure uniqueness and maintain a sorted collection.
A multiset, on the other hand, is similar to a set but allows duplicate elements. It also maintains the elements in a sorted order. Multisets are useful when duplicates are necessary, such as counting occurrences of items.
In this tutorial, we will explore how to use set and multiset containers in C++. We'll cover operations like insertion, finding, counting, and erasing elements, and understand the differences between these two containers.
A set is a container that stores unique elements following a specific order. The default sorting criterion is less than (<). Here's how you can declare and initialize a set:
1#include <iostream>2#include <set>34int main() {5std::set<int> mySet = {5, 2, 9, 1, 5};67for (const auto& elem : mySet) {8std::cout << elem << " ";9}1011return 0;12}
Explanation:
5 is automatically ignored by the set.To find an element in a set, you can use the find() method. This method returns an iterator to the element if found, or end() if not found:
1#include <iostream>2#include <set>34int main() {5std::set<int> mySet = {5, 2, 9, 1};67auto it = mySet.find(5);8if (it != mySet.end()) {9std::cout << "Element found: " << *it << std::endl;10} else {11std::cout << "Element not found" << std::endl;12}1314return 0;15}
Explanation:
count() method checks for the presence of an element and returns the number of times it appears, which is either 0 or 1.To remove an element from a set, you can use the erase() method. Here's how:
1#include <iostream>2#include <set>34int main() {5std::set<int> mySet = {5, 2, 9, 1};67mySet.erase(5);89for (const auto& elem : mySet) {10std::cout << elem << " ";11}1213return 0;14}
Explanation:
Inserting elements into a multiset works similarly to a set:
1#include <iostream>2#include <set>34int main() {5std::multiset<int> myMultiset;67myMultiset.insert(5);8myMultiset.insert(2);9myMultiset.insert(9);10myMultiset.insert(1);11myMultiset.insert(5); // Duplicate element1213for (const auto& elem : myMultiset) {14std::cout << elem << " ";15}1617return 0;18}
Explanation:
find() method returns an iterator to the first occurrence of the element.The count() method in a multiset can return more than one if there are duplicate elements:
1#include <iostream>2#include <set>34int main() {5std::multiset<int> myMultiset = {5, 2, 9, 1, 5};67int count = myMultiset.count(5);8std::cout << "Count of element 5: " << count << std::endl;910return 0;11}
Explanation:
erase() method removes all occurrences of the element.Let's create a practical example that demonstrates the use of both set and multiset. We'll implement a simple program to count the frequency of words in a text input:
1#include <iostream>2#include <string>3#include <set>4#include <map>56int main() {7std::multiset<std::string> wordMultiset;8std::set<std::string> uniqueWords;910std::cout << "Enter some text (type 'done' to finish):" << std::endl;11std::string input;12while (true) {13std::cin >> input;14if (input == "done") break;15wordMultiset.insert(input);16uniqueWords.insert(input);17}1819std::cout << "20Unique words and their frequencies:" << std::endl;21for (const auto& word : uniqueWords) {22int count = wordMultiset.count(word);23std::cout << word << ": " << count << std::endl;24}2526return 0;27}
Explanation:
multiset to store all words, allowing duplicates.set is used to store unique words for easy iteration and counting.| Concept | Description |
|---|---|
| Set | Stores unique elements in sorted order. |
| Multiset | Stores duplicate elements in sorted order. |
| Insertion | Use insert() method. |
| Finding | Use find() method to locate an element. |
| Counting | Use count() method to count occurrences of an element. |
| Erasing | Use erase() method to remove elements. |
In the next tutorial, we will explore Unordered Map & Unordered Multimap containers. These containers provide average constant-time complexity for most operations and are useful when order is not a concern.
Stay tuned!