Iterator is a generalization of a pointer.
scores.erase(scores.begin(), scores.begin()+2);
It erases the first and second elements.
old_v.insert(old_v.end(),new_v.begin()+1, new_v.end())
An iterator should have at least 4 properties.
- dereference *p
- assign an iterator to another. p=q
- compare one iterator to another for equality. p ==q and p != q
- ++p and –p
kind | usage | note |
---|---|---|
input iterators | read but not alter | a one-way iterator; it can increment, but it can’t back up |
output iterators | write but not read | single-pass, write-only |
forward iterators | only ++ operators; go forward through a container one element at a time | read and write |
bidirectional iterators | ++ and – | has all the features of forward iterators |
random access iterators | the ability to jump directly to an arbitrary element of a container | a+n; n+a;a[n];a |
find() function, by using a lowly input iterator, can be used with any container that contains readable valeus.
sort() function, by requiring a random access iterator, can be used just with containers that support that kind of iterator.
list doubly linked list, used a bidirectional iterator., it can’t use algorithms base on random access iterators.
STL algorithms can use pointers to operate on non-STL containers that based on pointers.
const int SIZE = 10;
double deceipts[SIZE];
sort(Receipts, Receipts + SIZE);