Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 42
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 42
42
Iterators
Iterators are types defined by STL Iterators are for containers like pointers are for ordinary data structures STL iterators provide pointer operations such as * and ++
Iterator Categories
Input Iterators Output Iterators Forward Iterators Bidirectional Iterators Random-access Iterators
Input Iterators
Can only read an element Can only move in forward direction one element at a time Support only one-pass algorithms
Output Iterators
Can only write an element Can only move in forward direction one element at a time Support only one-pass algorithms
Forward Iterators
Combine the capabilities of both input and output iterators
In addition they can bookmark a position in the container
Bidirectional Iterators
Provide all the capabilities of forward iterators In addition, they can move in backward direction
Iterator Summary
Random Access Bidirectional Forward Input Output
Associative Containers
-- set -- multiset -- map -- multimap
Iterator Operations
All Iterators
++p
pre-increment an iterator
p++
post-increment an iterator
Input Iterators
*p
Dereference operator (used as rvalue)
p1 = p2
Assignment
p1 == p2
Equality operator
p1 != p2
Inequality operator
p->
Access Operator
Output Iterators
*p
Dereference operator (can be used as lvalue)
p1 = p2
Assignment
Forward Iterators
Combine the operations of both input and output iterators
Bidirectional Iterators
Besides the operations of forward iterators they also support --p
Pre-increment operator
p- post-decrement operator
Random-access Iterators
Besides the operations of bidirectional iterators, they also support
p + i
Result is an iterator pointing at p + i
p i
Result is an iterator pointing at p i
Random-access Iterators
p += i
Increment iterator p by i positions
p = i
Decrement iterator p by i positions
p[ i ]
Returns a reference of element at p + i
p1 < p2
Returns true if p1 is before p2 in the container
Random-access Iterators
p1 <= p2
Returns true if p1 is before p2 in the container or p1 is equal to p2
p1 > p2
Returns true if p1 is after p2 in the container
p1 >= p2
Returns true if p1 is after p2 in the container or p1 is equal to p2
Sample Output
Vector contents: 1, 2, 3,
Sample Output
Set contents: 1, 2, 3,
Sample Output
Set contents: 3, 2, 1,
Algorithms
STL includes 70 standard algorithms These algorithms may use iterators to manipulate containers STL algorithms also work for ordinary pointers and data structures
Algorithms
An algorithm works with a particular container only if that container supports a particular iterator category
A multi-pass algorithm for example, requires bidirectional iterator(s) at least
Examples
Mutating-Sequence Algorithms
copy copy_backward fill fill_n generate generate_n iter_swap partition
Non-Mutating-Sequence Algorithms
adjacent_find count count_if equal find find_each find_end find_first_of
Numeric Algorithms
accumulate
inner_product partial_sum adjacent_difference
Output
1, 2, 3, 4, 5, 6,