I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlIterators

Bidirectional Iterator

Bidirectional iterator performs forward and backward moving
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Description

Bidirectional iterator is like a forward iterator with the difference that bidirectional iterator can be decremented.

Operations

Expression Description
*i Provides read access to the actual element
i->m Provides read access to a member of the actual element
++i Steps forward and returns new position (when not null)
i++ Steps forward and returns old position (when not null)
--i Steps backward and returns new position (when not null)
i-- Steps backward and returns old position (when not null)
i1==i2 Returns if i1 is equal to i2
i1!=i2 Returns if i1 is not equal to i2
TYPE() Creates iterator (default constructor)
TYPE(i) Copies iterator (copy constructor)
i1=i2 Assigns an iterator

References

Example:
Example - default list class iterator
Problem
This program illustrates typical uses of the default list class iterator (which is a bidirectional iterator), and shows the use of operators =, ++, --, != and * with list class iterators.
Workings
#include <iostream>
#include <string>
#include <list>
 
using namespace std;
 
int main()
{
  list<int> lst1(8);
 
  cout <<"\nFirst we enter some integer squares into "
         "a list of size 8, and then display: \n";
  //Create and initialize an iterator.
  list<int>::iterator p = lst1.begin();
  int i = 0;
 
  while(p != lst1.end())
  {
    *p = i*i; //Assign i*i to list lst via iterator p.
    p++;      //Advance the iterator **after** using it.
    i++;      //Increment the value to be squared.
  }
  p = lst1.begin();
  while(p != lst1.end()) 
    cout <<*p++<<" ";
 
  cout <<"\nNext we double the values in the list and "
         "re-display (backwards this time): \n";
  p = lst1.begin();
  while(p != lst1.end())
  {
    *p = *p * 2;
    p++;
  }
  //This time, initialize the iterator to lst1.end().
  //And decrement the iterator **before** using it!
   p = lst1.end();
   while(p != lst1.begin())
     cout <<*--p<<" "; //Note *--p "idiom".
 
  return 0;
}
Solution
Output:

First we enter some integer squares into a list of size 8, and then display:
0 1 4 9 16 25 36 49

Next we double the values in the list and re-display (backwards this time):
98 72 50 32 18 8 2 0
References