INFO
Associative container stores and retrieves element based on he value of “key”
- Key: Can be any data type (int, string, object, etc.)
- Order of insertion have no bearing on how they are organized
IMPORTANT
Associative container automatically recognize data in ascending order by default.
STL Sets and Multisets
- Set: each key must be unique
- Multiset: can contain duplicate keys
NOTE
Although elements in set are sorted in non-continuous memory (similar to Linked List) they are sorted in order → Can use Binary Search Tree (BSTs) to look for specific key
Constructors
Default Constructor
set<int> set1;Copy Constructor
set<int> set2(set1); // set2 not is a copy of set1Initializer List Constructor
set<int> set3{10, 20, 30, 40};
set<int> set4 = {1, 2, 3, 4, 5};Range Constructor
auto iterStart = set3.begin() + 2;
auto iterEnd = set3.end();
set<int> set5(iterStart, iterEnd);Member Functions
set::find(const T& val):- If
valis found in set → Return the iterator pointing to theval - If
valis not found in set → Return theset::end
- If
set::erase(): Removes element(s) in the set, reducing size of the setiterator erase(const_iterator position)- Delete by iterator
posiion - Return iterator to the element following the last element removed pointed by
position - All occurrence of element are deleted in multiset
- Return
set::endif last element in set is removed position: the iterator pointing to the element to be removed
- Delete by iterator
size_type erase(const T& val):- Return the number of elements deleted
val: value of element to the deleted- more used in multiset
iterator erase(const_iterator first, const_iterator last):- Return the iterator to the element the last element removed (
set::endif last element is removed) firstandlast: Range to be removed
- Return the iterator to the element the last element removed (