Bidirectional Iterators in C++

«
»
This C++ program demonstrates the use of bidirectional iterator on vector container. A bidirectional iterator is one which can sequentially iterate through a container in both the directions. C++ provides two such non-const iterators – reverse_iterator and iterator.

Here is the source code of the C++ program demonstrates bidirectional iterator on vector container. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Demonstrate Bidirectional Iterator on vector container
  3.  */
  4.  
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. int main()
  9. {
  10.     std::vector<int> v(10);
  11.     std::vector<int>::iterator i;
  12.     std::vector<int>::reverse_iterator ri;
  13.  
  14.     for (int i = 0; i < 10; i++)
  15.         v[i] = i;
  16.     // Straight traversal using iterator
  17.     std::cout << "Straight vector traversal using iterator "
  18. 	      << std::endl;
  19.     for (i = v.begin(); i != v.end(); i++)
  20.         std::cout << *i << "  "; 
  21.     // Straight traversal using reverse_iterator
  22.     std::cout << "\nStraight vector traversal using reverse_iterator "
  23. 	      << std::endl;
  24.     for (ri = v.rend() - 1; ri != v.rbegin() - 1; ri--)
  25.         std::cout << *ri << "  ";
  26.     // Reverse traversal using iterator
  27.     std::cout << "\nReverse vector traversal using iterator"
  28. 	      << std::endl;
  29.     for (i = v.end() - 1; i != v.begin() - 1; i--)
  30.         std::cout << *i << "  "; 
  31.     // Reverse traversal using reverse_iterator
  32.     std::cout << "\nReverse vector traversal using reverse_iterator "
  33. 	      << std::endl;
  34.     for (ri = v.rbegin(); ri != v.rend(); ri++)
  35.         std::cout << *ri << "  ";
  36. }

$ ./a.out
Straight vector traversal using iterator
0  1  2  3  4  5  6  7  8  9
Straight vector traversal using reverse_iterator
0  1  2  3  4  5  6  7  8  9
Reverse vector traversal using iterator
9  8  7  6  5  4  3  2  1  0
Reverse vector traversal using reverse_iterator
9  8  7  6  5  4  3  2  1  0

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.