⬅ Previous Next ➡

Standard Template Library (STL)

Standard Template Library (STL) in C++
  • STL (Standard Template Library) is a collection of ready-made generic classes and functions in C++.
  • Main STL components:
    • Containers - store data (vector, list, map, set, etc.)
    • Iterators - move through container elements
    • Algorithms - operations like sort, find, count
  • STL improves coding speed, reliability, and performance.

1) Containers (Types)

  • Sequence Containers: vector, list, deque
  • Container Adapters: stack, queue, priority_queue
  • Associative Containers: set, map (sorted)

2) Iterators (Concept)

  • Iterators behave like pointers to access container elements.
  • Common iterator functions: begin(), end().
  • Types: input, output, forward, bidirectional, random-access (depends on container).
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v = {10, 20, 30};

    for (auto it = v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

3) Algorithms (STL Algorithms)

  • STL provides algorithms in <algorithm>.
  • Common algorithms: sort(), find(), count(), reverse().
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {5, 2, 9, 1};

    sort(v.begin(), v.end());

    for (int x : v) cout << x << " ";
    cout << endl;

    return 0;
}

4) vector

  • vector is a dynamic array (resizable).
  • Fast random access, insertion at end is efficient.
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v;
    v.push_back(10);
    v.push_back(20);

    cout << "Size: " << v.size() << endl;
    cout << "First: " << v[0] << endl;

    return 0;
}

5) list

  • list is a doubly linked list.
  • Fast insert/delete at any position, but no direct indexing.
#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> L = {10, 20, 30};
    L.push_front(5);
    L.push_back(40);

    for (int x : L) cout << x << " ";
    cout << endl;

    return 0;
}

6) deque

  • deque (double-ended queue) allows insertion/removal at both ends.
#include <iostream>
#include <deque>
using namespace std;

int main() {
    deque<int> d;
    d.push_front(10);
    d.push_back(20);

    cout << d.front() << " " << d.back() << endl;
    return 0;
}

7) stack

  • stack is LIFO (Last In First Out) adapter.
  • Operations: push, pop, top.
#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> st;
    st.push(10);
    st.push(20);

    cout << "Top: " << st.top() << endl;
    st.pop();
    cout << "Top after pop: " << st.top() << endl;

    return 0;
}

8) queue

  • queue is FIFO (First In First Out) adapter.
  • Operations: push, pop, front, back.
#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<int> q;
    q.push(10);
    q.push(20);

    cout << "Front: " << q.front() << endl;
    q.pop();
    cout << "Front after pop: " << q.front() << endl;

    return 0;
}

9) set

  • set stores unique elements in sorted order.
  • Fast search (typically log n).
#include <iostream>
#include <set>
using namespace std;

int main() {
    set<int> s;
    s.insert(10);
    s.insert(5);
    s.insert(10); // ignored (duplicate)

    for (int x : s) cout << x << " ";
    cout << endl;

    return 0;
}

10) map

  • map stores key-value pairs with unique keys (sorted by key).
#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, string> m;
    m[1] = "One";
    m[2] = "Two";

    cout << "Key 1: " << m[1] << endl;
    return 0;
}

11) priority_queue

  • priority_queue gives highest priority element first (max-heap by default).
#include <iostream>
#include <queue>
using namespace std;

int main() {
    priority_queue<int> pq;
    pq.push(10);
    pq.push(50);
    pq.push(20);

    cout << "Top: " << pq.top() << endl; // 50
    pq.pop();
    cout << "Top after pop: " << pq.top() << endl; // 20

    return 0;
}

12) Quick Notes

  • vector: dynamic array, fast random access.
  • list: linked list, fast insert/delete.
  • deque: insert/delete both ends.
  • stack: LIFO, queue: FIFO.
  • set: unique sorted, map: key-value sorted.
  • priority_queue: highest element first.
⬅ Previous Next ➡