Maps and Sets in Cpp
diagram Maps Sets in Cpp
Maps and Sets are part of the associative containers in the Standard Template Library (STL) of C++. They are used to store data in a sorted and efficient manner with unique keys. These containers are ideal for fast searching, insertion, and deletion operations.
This article explores the concepts, types, and usage of maps and sets with examples, making it easy for beginners to understand.
Associative containers store elements in a key-based structure rather than a linear sequence. This allows faster lookup (logarithmic time complexity).
The two main types are:
Set – Stores unique elements only
Map – Stores key-value pairs (keys are unique)
A set is a collection of unique elements stored in sorted order by default.
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> numbers;
numbers.insert(5);
numbers.insert(2);
numbers.insert(8);
numbers.insert(2); // Duplicate, will be ignored
for (int n : numbers) {
cout << n << " ";
}
return 0;
}
Output: 2 5 8
Function | Description |
---|---|
insert() | Adds an element (ignores duplicates) |
erase() | Removes an element |
find() | Finds an element |
count() | Returns 1 if element exists |
size() | Returns number of elements |
empty() | Checks if set is empty |
Multiset is similar to a set but allows duplicate elements.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> ms = {1, 2, 2, 3};
ms.insert(2);
for (int n : ms) {
cout << n << " ";
}
return 0;
}
Output: 1 2 2 2 3
A map stores key-value pairs in sorted order by keys. Each key is unique.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> marks;
marks["Alice"] = 90;
marks["Bob"] = 85;
marks["Charlie"] = 92;
for (auto &pair : marks) {
cout << pair.first << ": " << pair.second << "\n";
}
return 0;
}
Output:
Alice: 90
Bob: 85
Charlie: 92
Function | Description |
---|---|
insert() | Inserts key-value pair |
erase() | Removes an element by key |
find() | Finds an element by key |
count() | Checks if key exists (1 if found) |
size() | Returns number of elements |
empty() | Checks if map is empty |
Multimap is similar to a map but allows multiple pairs with the same key.
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<string, int> scores;
scores.insert({"Alice", 90});
scores.insert({"Alice", 85});
for (auto &p : scores) {
cout << p.first << ": " << p.second << "\n";
}
return 0;
}
Feature | Set | Map |
---|---|---|
Data Structure | Stores only keys | Stores key-value pairs |
Duplicates | Not allowed (except multiset) | Not allowed (except multimap) |
Access | Uses iterators | Access via keys (map[key] ) |
Order | Sorted order | Sorted order by keys |
Use set when you only need to store unique items.
Use multiset if duplicates are required.
Use map when you need key-value mapping.
Use multimap when multiple values can be associated with a single key.
Maps and Sets are crucial STL containers in C++ for managing sorted collections and key-value data efficiently. They offer fast searching, insertion, and deletion, making them ideal for real-world applications like dictionaries, contact lists, and databases.