C++ Iterator

Welcome to The Coding College! In this tutorial, we’ll dive into iterators in C++. Iterators are an integral part of the Standard Template Library (STL) and provide a standard way to access elements of containers like arrays, vectors, sets, and maps.

What Is an Iterator?

An iterator is an object that acts as a pointer to elements in a container. It allows sequential access to the container’s elements without exposing the underlying representation.

Key Characteristics of Iterators:

  • Operate like pointers.
  • Provide sequential access to container elements.
  • Can traverse containers in different directions depending on the iterator type.

Declaring an Iterator

To use iterators, include the header of the container you want to work with (e.g., <vector> or <map>).

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int>::iterator it; // Declare an iterator for a vector

    return 0;
}

Types of Iterators

1. Input Iterator

Reads elements from a container but cannot modify them.

2. Output Iterator

Writes elements to a container but cannot read them.

3. Forward Iterator

Reads and writes elements while traversing in a single direction.

4. Bidirectional Iterator

Reads and writes elements while traversing in both directions.

5. Random Access Iterator

Provides direct access to elements using index-like operations.

Using Iterators

Example 1: Iterating Over a Vector

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30, 40};
    vector<int>::iterator it;

    for (it = nums.begin(); it != nums.end(); ++it) {
        cout << *it << " "; // Dereference the iterator to access the value
    }

    return 0;
}

Output:

10 20 30 40  

Example 2: Modify Elements Using an Iterator

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30, 40};
    vector<int>::iterator it;

    for (it = nums.begin(); it != nums.end(); ++it) {
        *it += 5; // Modify the value pointed by the iterator
    }

    for (it = nums.begin(); it != nums.end(); ++it) {
        cout << *it << " ";
    }

    return 0;
}

Output:

15 25 35 45  

Example 3: Reverse Iteration

Use a reverse iterator to iterate backward through a container.

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30, 40};

    for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
        cout << *it << " ";
    }

    return 0;
}

Output:

40 30 20 10  

Example 4: Using Iterators with Other Containers

Set

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet = {10, 20, 30};

    for (set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it) {
        cout << *it << " ";
    }

    return 0;
}

Output:

10 20 30  

Map

#include <map>
#include <iostream>
using namespace std;

int main() {
    map<int, string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

    for (map<int, string>::iterator it = myMap.begin(); it != myMap.end(); ++it) {
        cout << it->first << ": " << it->second << endl; // Access key and value
    }

    return 0;
}

Output:

1: One  
2: Two  
3: Three  

Common Iterator Functions

begin() and end()

  • begin(): Returns an iterator pointing to the first element.
  • end(): Returns an iterator pointing just past the last element.

rbegin() and rend()

  • rbegin(): Returns a reverse iterator pointing to the last element.
  • rend(): Returns a reverse iterator pointing just before the first element.

cbegin() and cend()

  • cbegin(): Returns a constant iterator to the first element.
  • cend(): Returns a constant iterator just past the last element.

Example: Find an Element Using Iterators

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30, 40};

    auto it = find(nums.begin(), nums.end(), 30);
    if (it != nums.end()) {
        cout << "Found 30 at position: " << distance(nums.begin(), it) << endl;
    } else {
        cout << "30 not found." << endl;
    }

    return 0;
}

Output:

Found 30 at position: 2  

Summary

  • Iterators provide a flexible and standardized way to traverse containers in C++.
  • Different types of iterators are available based on container and use case.
  • Iterators can be used for reading, modifying, and navigating elements efficiently.

Learn More at The Coding College

Visit The Coding College for more tutorials on C++ and STL!

Leave a Comment