CodeToLive

Standard Template Library (STL) in C++

The Standard Template Library (STL) is a powerful library in C++ that provides a collection of template classes and functions for common data structures and algorithms. It includes containers, iterators, and algorithms.

Containers

Containers are used to store collections of objects. Common STL containers include vector, list, map, and set.


#include <iostream>
#include <vector>

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

  for (int num : numbers) {
    std::cout << num << " ";
  }
  return 0;
}
      

Iterators

Iterators are used to traverse through the elements of a container. They act like pointers to elements in the container.


#include <iostream>
#include <vector>

int main() {
  std::vector<int> numbers = {10, 20, 30, 40, 50};

  for (auto it = numbers.begin(); it != numbers.end(); ++it) {
    std::cout << *it << " ";
  }
  return 0;
}
      

Algorithms

STL provides a wide range of algorithms that can be used with containers, such as sorting, searching, and modifying elements.


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

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

  // Sort the vector
  std::sort(numbers.begin(), numbers.end());

  for (int num : numbers) {
    std::cout << num << " ";
  }
  return 0;
}
      

Common STL Containers

Here are some commonly used STL containers:

Example: Using map

The map container stores elements as key-value pairs.


#include <iostream>
#include <map>

int main() {
  std::map<std::string, int> ages;
  ages["Alice"] = 30;
  ages["Bob"] = 25;

  for (const auto& pair : ages) {
    std::cout << pair.first << " is " << pair.second << " years old." << std::endl;
  }
  return 0;
}
      

Example: Using set

The set container stores unique elements in sorted order.


#include <iostream>
#include <set>

int main() {
  std::set<int> numbers = {5, 3, 1, 4, 2};

  for (int num : numbers) {
    std::cout << num << " ";
  }
  return 0;
}
      

Custom Comparators

You can define custom comparators for sorting or ordering elements in STL containers.


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

bool customCompare(int a, int b) {
  return a > b; // Sort in descending order
}

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

  // Sort with custom comparator
  std::sort(numbers.begin(), numbers.end(), customCompare);

  for (int num : numbers) {
    std::cout << num << " ";
  }
  return 0;
}
      

STL Algorithms

STL provides a variety of algorithms for operations like searching, counting, and transforming elements.


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

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

  // Find an element
  auto it = std::find(numbers.begin(), numbers.end(), 3);
  if (it != numbers.end()) {
    std::cout << "Element found: " << *it << std::endl;
  } else {
    std::cout << "Element not found." << std::endl;
  }

  // Count occurrences of an element
  int count = std::count(numbers.begin(), numbers.end(), 2);
  std::cout << "Count of 2: " << count << std::endl;

  // Transform elements (e.g., square each element)
  std::transform(numbers.begin(), numbers.end(), numbers.begin(), [](int x) {
    return x * x;
  });

  for (int num : numbers) {
    std::cout << num << " ";
  }
  return 0;
}
      

Advanced Containers

STL also includes advanced containers like unordered_map, unordered_set, and priority_queue.


#include <iostream>
#include <unordered_map>

int main() {
  std::unordered_map<std::string, int> ages;
  ages["Alice"] = 30;
  ages["Bob"] = 25;

  for (const auto& pair : ages) {
    std::cout << pair.first << " is " << pair.second << " years old." << std::endl;
  }
  return 0;
}
      
Next: Smart Pointers in C++