Set, Multiset, Iterator (iterators) Comments

Set, Multiset, Iterator (iterators)


Iterator: Iterator

We can find some of the so-called data structures such as arrays and linked lists, they have some similar properties. We look at the following two examples:

  1. Array: definition array \ (int ~ A [10] \) , a pointer to the first element is \ (A \) , the second element is a pointer \ (A +. 1 \) , the third element is a pointer \ (A + 2 \) , and the like,
  2. Chain: For a list \ (List \ text {<} int \ {text> mylist} ~; \) , which is a chain of storage method is stored, the memory addresses are not continuous, but dispersed, it can only with \ (next \) or \ (last \) to access elements.

In order to unify the two pointer storage methods, we introduced a more advanced pointer, called \ (Iterator \) , now define a \ (Iterator \) : \ (std \ text {::} the SET \ text {< int} \ {text> :: ~} Iterator ITER \) , this pointer may support the following operations:

  1. \ (ITER \ {text} ++ \) : the \ (ITER \) the next address points to an element
  2. \ (iter- \ hspace {0.2pt} - \) : the \ (ITER \) a point on the element address
  3. \ (ITER * \) : to obtain \ (ITER \) pointer points to the address value stored

Set / Multiset

\ (Set \) is a collection that has the nature of a collection owned by: uniqueness of the element. And \ (Multiset \) no aforesaid nature, there will be of the form: \ (\ {0,0,1,1,2 \} \) such sets.

Both default data structure is performed in ascending order , similar to using a balanced binary search tree.

The following is a use \ (iterator \) examples:

#include <iostream>
#include <set>
#include <algorithm>

int main() {
    std::set<int> s;
    s.insert(2); s.insert(1); s.insert(10);
    std::set<int>::iterator iter = s.end();
    iter --;
    std::cout << *iter << std::endl; // 10
    iter --;
    std::cout << *iter << std::endl; // 2
    iter --;
    std::cout << *iter << std::endl; // 1
}

By way of example above, not difficult to find, \ (End () \) points are not present in the \ (SET \) of an address, an address is the last element.

Note:

Almost all \ (set \) function and the value returned is \ (Iterator \) .

All \ (lower \ _bound () \ ) and \ (upper \ _bound () \ ) is to follow the left and right to open and close , i.e. \ ([a, b) \ ) in the form of

For example:

  1. For collection \ (\ {1,2,3,4,5,6,8,9 \} \)

    \(*lower\_bound(7) = *upper\_bound(7) = 8\)

  2. For arrays \ (\ {1,2,3,3,3,3,3,4 \} \)

    \(*upper\_bound(3) - *lower\_bound(3) = count(3) = 5\)

  3. For collection \ (\ {1,2,3,4,5,6,7,8,9 \} \)

    \(lower\_bound(10) = upper\_bound(10) = set.end()\)

Guess you like

Origin www.cnblogs.com/jeffersonqin/p/11210915.html