Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

STL Iterators - Comprehensive Tutorial

Introduction to STL Iterators

Iterators are a fundamental part of the Standard Template Library (STL) in C++. They provide a way to access elements of a container (like vectors, lists, and maps) without exposing the underlying representation. This allows for more generic and reusable code.

Types of Iterators

There are several types of iterators in STL:

  • Input Iterators: Read from the container.
  • Output Iterators: Write to the container.
  • Forward Iterators: Read and write, move forward.
  • Bidirectional Iterators: Read and write, move forward and backward.
  • Random Access Iterators: Read and write, move to any element directly.

Basic Iterator Operations

Here are some basic operations you can perform with iterators:

  • Increment: ++it or it++
  • Decrement: --it or it--
  • Dereference: *it
  • Member Access: it->member

Example: Using Iterators with Vectors

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << " ";
    }
    
    return 0;
}
                
Output: 1 2 3 4 5

This example demonstrates how to use iterators to traverse a vector and print its elements.

Example: Using Const Iterators

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    for (std::vector<int>::const_iterator it = vec.cbegin(); it != vec.cend(); ++it) {
        std::cout << *it << " ";
    }
    
    return 0;
}
                
Output: 1 2 3 4 5

This example demonstrates how to use const iterators to traverse a vector and print its elements. Const iterators do not allow modification of the elements they point to.

Example: Using Reverse Iterators

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    for (std::vector<int>::reverse_iterator rit = vec.rbegin(); rit != vec.rend(); ++rit) {
        std::cout << *rit << " ";
    }
    
    return 0;
}
                
Output: 5 4 3 2 1

This example demonstrates how to use reverse iterators to traverse a vector in reverse order and print its elements.

Example: Using Iterators with Other Containers

Iterators can be used with other STL containers like lists, maps, and sets. Here is an example with a list:

#include <iostream>
#include <list>

int main() {
    std::list<int> lst = {1, 2, 3, 4, 5};
    
    for (std::list<int>::iterator it = lst.begin(); it != lst.end(); ++it) {
        std::cout << *it << " ";
    }
    
    return 0;
}
                
Output: 1 2 3 4 5

Advanced Iterator Techniques

Beyond basic usage, iterators can be used in more advanced ways. For instance, combining iterator operations with STL algorithms:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::vector<int> result(vec.size());
    
    std::copy(vec.begin(), vec.end(), result.begin());
    
    for (std::vector<int>::iterator it = result.begin(); it != result.end(); ++it) {
        std::cout << *it << " ";
    }
    
    return 0;
}
                
Output: 1 2 3 4 5

This example demonstrates how to use the std::copy algorithm with iterators to copy elements from one vector to another.

Summary

STL iterators are powerful tools for accessing and manipulating elements in containers. Understanding the different types of iterators and how to use them effectively can greatly enhance your ability to write efficient and reusable C++ code.