3. Iterators - C++ Feature #
Created Tuesday 18 February 2020
- Traversing containers requires container.
They are useful for vector, list, map, set, unordered_map, unordered_set.
Info about STL:
-
set is nothing but a map with no values. Hence duplicate keys don’t have a meaning. map has a pair. No difference as such.
-
unordered_map works similar to unordered_set. O(1) ops. hash table.
-
set works similar to map. Both are ordered and implemented using BST. O(logn) ops.
-
Iterators are multi-purpose. They can be used for deleting as well as traversing.
-
Also iterators have container dependent implementations.
- The way to think of an iterator
- An iterator is like a pointer to the data element. i.e -> and * should be used.
- syntax: container_type : : iterator iterator_name;
- We advance forward using iterator_name+=1; i.e adding integers is allowed.
- container_name.begin() returns iterator to the first element.
- container_name.end() returns the iterator after the last element.
Many functions in STL deal with iterators.
- find(value), returns iterator to the first found element.
- erase(value_) or erase(start_iter, ending_iter) - does not make much sense for unordered map. Useful for list, vector.