A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_iterator_iterator.htm below:

C++ Iterator Overview

C++ Iterator Library - <iterator> Description

It is an iterator base class.

Declaration

Following is the declaration for std::iterator.

C++11
template <class Category,              
          class T,                     
          class Distance = ptrdiff_t,  
          class Pointer = T*,          
          class Reference = T&> 
		  class iterator;
Parameters Return value

none

Exceptions

If x somehow throws while applying the unary operator& to it, this function never throws exceptions.

Time complexity

constant for random-access iterators.

Example

The 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