📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page
  • iterator
  • const_iterator
  • reverse_iterator
  • const_reverse_iterator

Was this helpful?

  1. Chapter 8: Standard Template Library

More about Iterators

As we have already known, iterators are useful to traverse elements in a STL container. However, there are also other types of iterators that are commonly used.

iterator

iterator is the normal type of iterators we have learned before. It supports forward traversal of containers. We can use the deference to access the element it points to, and modify it as well.

int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::iterator it;
    for (it = v.begin(); it != v.end(); ++it) {
        *it += 1;
        cout << *it << " "; // 1 2 3 4 5 6 7 8 9 10
    }
    return 0;
}

const_iterator

const_iterator supports forward traversal of containers. We can use the deference to access the element it points to, but cannot modify it, since the deference returns a const reference to the element.

int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::const_iterator it;
    for (it = v.begin(); it != v.end(); ++it) {
        *it += 1;   // ERROR
        cout << *it << " ";
    }
    return 0;
}

reverse_iterator

reverse_iterator supports backward traversal of containers. We can use the deference to access the element it points to, and modify it as well.

Instead of begin() and end() in normal iterators, we use rbegin() and rend() in reverse iterators. rbegin() returns a reverse_iterator to the last element in the container, while rend() returns a reverse_iterator to the one before the first element in the container.

int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::reverse_iterator it;
    for (it = v.rbegin(); it != v.rend(); ++it) {
        *it += 1;
        cout << *it << " "; // 10 9 8 7 6 5 4 3 2 1
    }
    return 0;
}

const_reverse_iterator

reverse_iterator supports backward traversal of containers. We can use the deference to access the element it points to, but cannot modify it, since the deference returns a const reference to the element.

int main() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    vector<int>::const_reverse_iterator it;
    for (it = v.rbegin(); it != v.rend(); ++it) {
        *it += 1;   // ERROR
        cout << *it << " ";
    }
    return 0;
}
PreviousAssociative ContainersNextFunction Objects

Last updated 4 years ago

Was this helpful?