STL & Templates

Master C++ STL containers, algorithms, iterators, and template programming techniques essential for competitive programming. Learn to leverage the standard library for efficient and clean code solutions.

1. STL Containers Overview

STL provides various containers optimized for different use cases. Understanding their time complexities and appropriate usage is crucial for competitive programming.

Common STL Containers

#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <priority_queue>
using namespace std;

int main() {
    // Sequential containers
    vector<int> vec = {1, 2, 3, 4, 5};
    deque<int> dq = {1, 2, 3, 4, 5};
    list<int> lst = {1, 2, 3, 4, 5};
    
    // Associative containers
    set<int> s = {5, 2, 8, 1, 9};
    map<string, int> m = {{"apple", 5}, {"banana", 3}};
    
    // Unordered containers (hash-based)
    unordered_set<int> us = {1, 2, 3, 4, 5};
    unordered_map<string, int> um = {{"key1", 10}, {"key2", 20}};
    
    // Container adapters
    stack<int> st;
    queue<int> q;
    priority_queue<int> pq; // max heap by default
    
    // Demonstrate basic operations
    cout << "Vector size: " << vec.size() << endl;
    cout << "Set contains 5: " << (s.count(5) > 0) << endl;
    cout << "Map value for 'apple': " << m["apple"] << endl;
    
    return 0;
}

2. STL Algorithms

STL algorithms provide efficient implementations of common operations. They work with iterators and can be applied to various container types.

Essential STL Algorithms

3. Iterators and Iterator Categories

Iterators provide a uniform interface to access container elements. Understanding iterator categories helps choose the right algorithms and containers.

Iterator Usage Examples

4. Template Programming Basics

Templates enable generic programming, allowing you to write code that works with different data types. This is essential for creating reusable competitive programming solutions.

Function and Class Templates

5. Advanced Template Techniques

Advanced template features like variadic templates, SFINAE, and template metaprogramming can create powerful, flexible solutions for competitive programming.

Advanced Template Features

6. Competitive Programming Applications

Practical applications of STL and templates in competitive programming scenarios, including common patterns and optimizations.

Competitive Programming Template

Summary

  • STL Containers: Choose the right container based on required operations and time complexity
  • STL Algorithms: Leverage built-in algorithms for sorting, searching, and manipulation
  • Iterators: Understand iterator categories and use them effectively with algorithms
  • Templates: Write generic, reusable code using function and class templates
  • Advanced Features: Use variadic templates, SFINAE, and metaprogramming for complex solutions
  • Competitive Programming: Apply STL and templates to solve problems efficiently