C++ Standard Template Library (STL): Basics of Containers, Algorithms, and Iterators

C++ Standard Template Library (STL): Basics of Containers, Algorithms, and Iterators

The C++ Standard Template Library (STL) is a powerful toolkit that provides a range of efficient components to help us implement complex data structures and algorithms more easily. STL mainly consists of three parts: containers, algorithms, and iterators. This article will introduce the basic concepts and usage of these three parts in detail for beginners, with code examples for demonstration.

1. Containers

Containers are data structures used to store objects. STL provides several different types of containers, each with specific uses, including:

  • Sequential Containers: such as <span>vector</span>, <span>deque</span>, and <span>list</span>
  • Associative Containers: such as <span>set</span>, <span>map</span>, and their unordered versions
  • Adapters: such as stacks (stack) and queues (queue)

1.1 Sequential Containers

1.1.1 vector

The <span>vector</span> in C++ is a dynamic array that can automatically resize and supports fast random access.

#include <iostream>
#include <vector>
int main() {
    std::vector<int> vec;
    // Add elements
    vec.push_back(10);
    vec.push_back(20);
    vec.push_back(30);
    // Iterate and print elements
    for (int i = 0; i < vec.size(); ++i) {
        std::cout << "vec[" << i << "] = " << vec[i] << std::endl;
    }
    return 0;
}

Output:

vec[0] = 10
vec[1] = 20
vec[2] = 30

1.1.2 list

Unlike <span>vector</span>, the <span>list</span> is a doubly linked list that allows fast insertion or deletion of elements at any position, but does not support random access.

#include <iostream>
#include <list>
int main() {
    std::list<int> lst;
    // Add elements
    lst.push_back(10);
    lst.push_back(20);
    // Insert element at the front
    lst.push_front(5);
    // Iterate and print elements
    for (const auto& elem : lst) {
        std::cout << elem << " ";
    }
    return 0;
}

Output:

5 10 20 

1.2 Corresponding Containers – map/set

map Example

#include <iostream>
#include <map>
int main() {
   std::map<std::string, int> age_map;
   age_map["Alice"] = 25;
   age_map["Bob"] = 30;
   for(const auto& pair : age_map) {
       std::cout << pair.first << ": " << pair.second << "\n";
   }
   return 0;
}

Output:

Alice: 25  Bob: 30 

2. Algorithms

STL provides many generic algorithms, such as sorting and searching. These functions typically take iterators as parameters to operate on different types of containers.

Sorting Algorithm Example

The <span>sort()</span> algorithm defined in STL can be used to sort data in a vector:

#include <iostream>
#include <vector>
#include <algorithm> // Include algorithm header to use sort
int main() {
     std::vector<int> numbers {4, 2, 3, 5, 7, 6};
     std::sort(numbers.begin(), numbers.end());
     for (const auto &num : numbers) {
          std::cout << num << " ";
      }
}

Leave a Comment