STL--iterator

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.

  1. dereference *p
  2. assign an iterator to another. p=q
  3. compare one iterator to another for equality. p ==q and p != q
  4. ++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);

Reverse pointers solve both problems by decrementing first and then dereferencing. That is, *rp dereferences the iterator immediately preceding the current value of *rp. If rp points to position six, *rp is the value of position five.

猜你喜欢

转载自blog.csdn.net/lilele12211104/article/details/79713783