Working with STL Containers and Iterators

Tutorial 4 of 5

Introduction

In this tutorial, we will focus on learning how to use the Standard Template Library (STL) containers and iterators in C++ programming. STL is a powerful feature in C++ that provides predefined templates for a set of common data structures and algorithms.

Goals

By the end of this tutorial, you will be able to:

  1. Understand what STL containers and iterators are.
  2. Use different types of STL containers like vectors, lists, sets etc.
  3. Use STL iterators to access and manipulate data in STL containers.

Prerequisites

This tutorial assumes you have a basic knowledge of C++ syntax and programming concepts. Familiarity with basic data structures will be helpful but not necessary.

Step-by-Step Guide

STL Containers

STL containers are template-based data structures. They can store any data type that can be copied or moved, like integers, floating point numbers, custom objects, etc. The primary STL container types are:

  1. Sequence containers (vector, list, deque, array, forward_list): These containers implement data structures which can be accessed sequentially.

  2. Container adaptors (queue, priority_queue, stack): These are not full container classes, but wrappers that provide a specific interface relying on an object of one of the container classes.

  3. Associative containers (set, multiset, map, multimap): These containers implement sorted data structures that can be quickly searched.

  4. Unordered Associative containers (unordered_set, unordered_multiset, unordered_map, unordered_multimap): These containers implement unsorted (hashed) data structures that can be quickly searched.

STL Iterators

Iterators in STL provide a way to access the elements in a container sequentially without exposing the underlying structure of the container. Iterators essentially act like pointers.

Code Examples

Using Vector (STL Container)

#include <iostream>
#include <vector>

int main() {
    // Declare a vector
    std::vector<int> vec;

    // Add elements to the vector
    vec.push_back(10);
    vec.push_back(20);
    vec.push_back(30);

    // Access elements using an iterator
    for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
        std::cout << *it << " ";
    }

    return 0;
}

Output

10 20 30

In this example, we declare a vector vec and add elements using push_back(). We then use an iterator (it) to access and print each element in the vector.

Using Set (STL Container)

#include <iostream>
#include <set>

int main() {
    // Declare a set
    std::set<int> s;

    // Add elements to the set
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(20); // This will not be added, as sets do not allow duplicate elements

    // Access elements using an iterator
    for(std::set<int>::iterator it = s.begin(); it != s.end(); it++) {
        std::cout << *it << " ";
    }

    return 0;
}

Output

10 20 30

In this example, we declare a set s and add elements using insert(). Notice that attempting to insert a duplicate element has no effect, as sets do not allow duplicate elements. We then use an iterator to access and print each element in the set.

Summary

In this tutorial, we have learned about STL containers and iterators in C++. We've seen how to declare, populate, and access data in several types of STL containers using iterators. For further exploration of STL, consider learning about different types of iterators (like forward iterators, bidirectional iterators, etc.) and other STL components like algorithms and functors.

Practice Exercises

  1. Exercise 1: Create a std::list of integers and print its elements in reverse order.

  2. Exercise 2: Create a std::map where keys are names (strings) and values are ages (integers). Insert some data in the map and print all names with their corresponding ages.

  3. Exercise 3: Create a std::unordered_set of integers, insert some data into it and print all unique elements (an unordered_set only stores unique elements).

Solutions and explanations will be provided upon request.

Further Resources

  1. C++ Reference
  2. Learn C++
  3. C++ STL Tutorial