It is an iterator base class.
DeclarationFollowing is the declaration for std::iterator.
C++11template <class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&> class iterator;Parameters
T − It indicates about type of element.
Distance − It represents the difference between two iterators.
Pointer − It represents a pointer to an element pointed by the iterator.
Reference − It represents a reference to an element pointed by the iterator.
none
ExceptionsIf x somehow throws while applying the unary operator& to it, this function never throws exceptions.
Time complexityconstant for random-access iterators.
ExampleThe following example shows the usage of std::iterator.
#include <iostream> #include <iterator> class MyIterator : public std::iterator<std::input_iterator_tag, int> { int* p; public: MyIterator(int* x) :p(x) {} MyIterator(const MyIterator& mit) : p(mit.p) {} MyIterator& operator++() {++p;return *this;} MyIterator operator++(int) {MyIterator tmp(*this); operator++(); return tmp;} bool operator==(const MyIterator& rhs) {return p==rhs.p;} bool operator!=(const MyIterator& rhs) {return p!=rhs.p;} int& operator*() {return *p;} }; int main () { int numbers[] = {1,2,3,4,5}; MyIterator from(numbers); MyIterator until(numbers+5); for (MyIterator it = from; it!=until; it++) std::cout << *it << ' '; std::cout << '\n'; return 0; }
Let us compile and run the above program, this will produce the following result −
1 2 3 4 5
iterator.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4