c-plus-plus
  1. c-plus-plus-iterators

C++ Iterators

Iterators in C++ are used to traverse through containers like arrays, vectors, and even data structures like linked lists and trees. They provide a way to access and manipulate the elements within these containers.

Syntax

container_type::iterator iter_name;
container_type::const_iterator const_iter_name;

The first line creates an iterator for a container that can be used to modify the elements within the container. The second line creates a constant iterator that can be used to read the elements within the container but not modify them.

Example

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

int main() {
  vector<int> my_vector = {1, 2, 3, 4, 5};

  // iterator example
  vector<int>::iterator iter;
  for (iter = my_vector.begin(); iter != my_vector.end(); ++iter) {
    cout << *iter << " ";
  }
  cout << endl;

  // constant iterator example
  vector<int>::const_iterator const_iter;
  for (const_iter = my_vector.begin(); const_iter != my_vector.end(); ++const_iter) {
    cout << *const_iter << " ";
  }
  cout << endl;

  return 0;
}

Output

1 2 3 4 5
1 2 3 4 5

Explanation

In the above example, we have a vector of integers named "my_vector". We create both an iterator and a constant iterator to traverse through the vector. We use a for loop to iterate through the vector, incrementing the iterator at each step. We use the dereference operator (*) to get the value at the current iterator position.

Use

Iterators are used to traverse through containers and perform operations like insert, delete, and modify elements within those containers.

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

int main() {
  list<int> my_list = {1, 2, 3, 4, 5};
  list<int>::iterator iter;

  // erase even numbers from list
  for (iter = my_list.begin(); iter != my_list.end(); ) {
    if (*iter % 2 == 0) {
      iter = my_list.erase(iter);
    } else {
      ++iter;
    }
  }

  // print list
  for (iter = my_list.begin(); iter != my_list.end(); ++iter) {
    cout << *iter << " ";
  }
  cout << endl;

  return 0;
}

In the above example, we use an iterator to traverse through a linked list and remove all even numbers from the list. We use the erase() function of the linked list container to delete the even numbers. We then print the updated linked list using the iterator.

Important Points

  • Iterators provide a way to traverse through containers like arrays, vectors, linked lists, and trees.
  • There are two types of iterators: iterators that can modify container elements, and constant iterators that can only read container elements.
  • The syntax for creating an iterator is container_type::iterator iter_name.
  • Iterators can be used to insert, delete, and modify container elements.

Summary

In summary, iterators in C++ are essential for traversing through containers like arrays, vectors, linked lists, and trees. They provide a way to access and modify elements within these containers. There are two types of iterators: iterators that can modify container elements, and constant iterators that can only read container elements. The syntax for creating an iterator is container_type::iterator iter_name, and they can be used to insert, delete, and modify container elements.

Published on: