Week 5 - Associative Containers: sets and maps
Dec 18, 2015
Week 5- Associative Containers:
sets and maps
22 Main IndexMain Index
ContentsContents
Container TypesSequence Containers
Adapter Containers
Associative Containers
Vector Stack Set, Multiset
Deque Queue Map, Mutltimap
List Priority Queue
Associative containers
• Designed to be especially efficient in accessing its elements by their key, as opposed to sequence containers which are more efficient in accessing elements by their position.
• A programmer can use the key without concern for how elements are physically stored and how operations make the association between the key and an element.
• Insertion, deletion, and testing whether an element is in it, in logarithmic time - O(log n). Implemented using binary search trees.
Sets
• In a set, the data value is just the key:
C++ Set
• Sets are containers that store unique elements following a specific order.
• The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
• Multisets are containers where multiple elements can have equivalent values.
Iterators
• Set containers keep their elements in ascending order, which follows the container's sorting criterion (By default, this is a less operator<).
• Hence, begin() points at the smallest element in the set, and end() points just past the largest element in the set.
77 Main IndexMain Index ContentsContents
CLASS set Operations <set>
iterator begin();Return an iterator pointing at the first member in
the set.
iterator end();Return an iterator pointing just past the last
member in the set.
88 Main IndexMain Index ContentsContents
CLASS set Constructors <set>
set();Create an empty set. This is the Default
Constructor.set(T *first, T *last);
Initialize the set by using the address range [first, last).CLASS set Operations <set>
bool empty() const;Is the set empty?
int size() const;Return the number of elements in the set.
99 Main IndexMain Index ContentsContents
CLASS set Operations <set>
int count(const T& key) const;Search for key in the set and return 1 if it is in the
set and 0 otherwise.
iterator find(const T& key);Search for key in the set and return an iterator
pointing at it, or end() if it is not found.
1010 Main IndexMain Index ContentsContents
CLASS set Operations <set>
pair<iterator, bool> insert(const T& key);If key is not in the set, insert it and then return a
pair whose first element is an iterator pointing to the new element and whose second element is true. Otherwise, return a pair whose first element is an iterator pointing at the existing element and whose second element is false.
Postcondition: The set size increases by 1 if key is
not in the set.int erase(const T& key);
If key is in the set, erase it and return 1; otherwise, return 0.
Postcondition: The set size decreases by 1 if key is
in the set.
1111 Main IndexMain Index ContentsContents
CLASS set Operations <set>
void erase(iterator pos);Erase the item pointed to by pos.
Preconditions: The set is not empty, and pos points
to a valid set element.Postcondition: The set size decreases by 1.
void erase(iterator first, iterator last);Erase the elements in the range [first, last).
Precondition: The set is not empty.Postcondition: The set size decreases by the
number of elements in the range.
Set Operations
Set Operations
Set Operations
Example
Implementing Set Intersection
Complexity: Up to linear O(N)at most 2*(count1+count2)-1 comparisons(where countX is the distance between firstX and lastX)
C++ implementation
Maps
C++ Map• Maps are associative containers that store elements formed by
a combination of a key value and a mapped value, following a specific order.
• The key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key.
• The types of key and mapped value may differ, and are grouped together in a pair type.
• Multimaps are associative containers where multiple elements can have equivalent keys.
Maps• A map stores an element as a <key-value> pair,
The index operator [] leads us to refer to a map as an associative array. The index (key) for a map is not limited to integer values, but can be of any type.
If k does not match the key of any element in the container, the function inserts a new element.
Map iterators
insert()
find() and erase()
27
Binary search trees
sets, maps
The storage structure should be selected so thatthe container operations can be implementedefficiently
Reading & HW reminder
• Book chapter 4.8
28
HW3 is due this Friday. (Submission before Thursday will receive 20
additional points)