Week 10_ STLs III
Week 10_ STLs III
Agenda
1. Associative containers
○ Associative Properties
○ Prerequisite: Pair
○ Set
○ Multiset
○ Map
○ Multimap
2. Important methods
3. Unordered containers
1. Associative containers
● Associative containers are special containers that keep their elements
sorted at all times.
● When we add a new element, the container automatically finds the best
place to put it in to maintain the sorted order. Unlike sequence containers,
we cannot specify exactly where the new element should go, as the
container has its own way of organizing the elements. Instead, we wait
for the container to do its job and return an iterator pointing to the new
element's position.
a. Associative Properties
Property Associative
Size Dynamic
Random Access No
Search O(log n)
Insert O(log n)
Erase O(log n)
Before we dive into associative containers, let's first discuss a simple
container called pair. As its name suggests, it holds a pair of values. When
we create a pair, we specify two types for it - one for the first value and one
for the second value. Let's look at an example to understand it better.
b. Pair
pair<string, int> p;
p.first = "ahmed";
p.second = 20;
p = make_pair("ahmed", 20);
● Set
● Multiset
● Map
● Multimap
c. Set
#include <set>
set <int> s;
i. Set insert
set<int> s;
/* insert function returns a pair, where the first is the
value to be inserted and the second is boolean indicating
whether the value is added or was already exist
*/
s.insert(2);
s.insert(0);
s.insert(1);
s.insert(0);
auto it = s.find(10);
if(it!= s.end())
cout << "Found" << endl;
else
cout << "Not Found" << endl;
if(s.count(10))
cout << "Found" << endl;
else
cout << "Not Found" << endl;
// range-based method
for(auto it: s)
cout << it << " ";
int countDistinct(vector<element> v) {
set<element> st;
for (auto elem : v)
st.insert(elem);
return st.size();
}
void removeDuplicates(vector<element> v) {
set<element> st;
for (auto elem: v)
st.insert(elem);
for(auto elem : st)
cout << elem << " ";
}
d. Multiset
#include <set>
multiset<int> ms;
ms.insert(2);
ms.insert(0);
ms.insert(0);
ms.insert(2);
ms.erase(ms.find(2));
Do not use the count function if u just need to check whether the
element exist or not.
e. Map
#include <map>
auto it = fruits.find("Apple");
if (it != fruits.end())
cout << (*it).first << " " << it->second << endl;
//prints Ahmed 9
// range-based method
for(auto it : mp)
cout << it.first << " " << it.second << endl;
iii. Map Use Cases
void countFrequency(vector<element> v) {
map<element, int> freq;
for (int i = 0; i < (int)v.size(); i++)
freq[v[i]]++;
for (auto elem: freq)
cout << elem.first << " occurs " << elem.second
<< " times" << endl;
}
#include <map>
multimap<key_type,value_type> name;
fruits.erase("Apple");
// removes all occurrences of key
🞿 Do not use the count function if u just need to check whether the
element exist or not.
2. Important methods
Priority
Vector Deque Stack Queue Set Map
Queue
size size size size size size size
insert
erase
3. Unordered containers
Unordered containers are associative containers that were introduced in
C++11. They use a concept called hashing to optimize the average
complexity of search, insertion, and deletion operations to O(1). However, in
the worst case, the complexity can still be O(n). While we are not always
interested in using unordered containers for competitive programming, they
can be useful in real-life applications and in some cases in competitions
when we are confident that our algorithm will not encounter worst-case
scenarios.
● unordered_set
● unordered_multiset
● unordered_map
● unordered_multimap