CSS Prepare

optional · CSS study notes

17. Standard Template Library (STL)

  • A C++ library providing generic, reusable data structures and algorithms
  • Containers: vector (dynamic array), list (doubly linked list), map (key-value pairs, typically balanced-tree based), set (unique sorted elements), stack, queue
  • Iterators: generalized pointers used to traverse container elements uniformly
  • Algorithms: pre-built functions (e.g., sort(), find(), count()) that operate generically over containers via iterators
#include <vector>
#include <algorithm>
vector<int> nums = {5, 2, 8, 1, 9};
sort(nums.begin(), nums.end());   // 1 2 5 8 9
NeedContainer
Fast random access, dynamic sizevector
Frequent insert/delete in the middlelist
Key-value lookup, sorted keysmap
Unique elements, sortedset
LIFO accessstack
FIFO accessqueue

Why STL exists

Before STL, every C++ programmer wrote their own linked list, their own sort function, etc. — repetitive and error-prone. STL provides pre-built, well-tested, generic (template-based) versions of the data structures and algorithms used in almost every program.

The three pieces, working together

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    vector<int> nums = {5, 2, 8, 1, 9};   // CONTAINER

    sort(nums.begin(), nums.end());       // ALGORITHM, operating via ITERATORS (begin/end)

    for (int n : nums)                    // range-based loop, uses iterators internally
        cout << n << " ";                 // 1 2 5 8 9

    auto it = find(nums.begin(), nums.end(), 8);   // ALGORITHM again
    if (it != nums.end())
        cout << "\nFound 8 at position " << (it - nums.begin());   // position 3

    return 0;
}

map — key-value pairs, worked example

#include <map>
map<string, int> ages;
ages["Ali"] = 25;
ages["Sara"] = 30;
cout << ages["Ali"];   // 25
for (auto& pair : ages)
    cout << pair.first << ": " << pair.second << endl;   // iterates in sorted key order

Why iterators matter — the generalization

An iterator behaves like a pointer (*it dereferences, ++it advances) but works uniformly across very different containers (vector, list, map...). This is why sort(nums.begin(), nums.end()) can work on a vector while the same style of call works on other containers too — the algorithm doesn't need to know the container's internal structure, only how to move an iterator.

Important MCQs

  1. Which STL container provides fast random access and dynamic resizing?
    • a) list b) vector c) map d) set
  2. Which STL container stores unique, sorted elements?
    • a) vector b) list c) set d) queue
  3. STL algorithms like sort() operate generically over containers using:
    • a) Pointers only b) Iterators c) Static members d) Templates alone, without iterators
  4. Which container is best suited for key-value lookups with sorted keys?
    • a) vector b) map c) stack d) queue

Self-Test Questions

  1. What are the three core pieces of STL, and how do they work together?
  2. Why does using iterators let the same sort() function work across different container types?
  3. When would map be a better fit than vector for storing data?

📌 Sample & Repeated FPSC Questions (2016–2026)

2026, Paper I, Q10 (1 mark, MCQ) — What does the Standard Template Library (STL) provide in C++?

  • A) Pre-defined classes for GUI
  • B) Pre-defined functions for math operations
  • C) Pre-defined containers and algorithms ✅ Correct
  • D) None of these

Explanation: STL provides generic, reusable container classes (vector, list, map, set, etc.) along with a rich set of generic algorithms (sort, find, etc.) and iterators to work with them — exactly the three-piece breakdown (containers / iterators / algorithms) covered above.

Note: across all CSS CS past papers 2016–2026, this is the only question that isolates STL as its own topic — FPSC otherwise tests STL knowledge indirectly, inside larger data-structure/algorithm-design questions (e.g. using vector/queue as the underlying container while implementing BFS) rather than as a standalone "what is STL" question. Don't skip this page just because it's rarely asked directly — it's assumed knowledge in coding questions elsewhere.

📜 Past-Paper Questions & MCQs (CSS 2016–2026)

MCQs

2026 P-I · Q10. What does the Standard Template Library (STL) provide in C++?

  • (A) Pre-defined classes for GUI
  • (B) Pre-defined functions for math operations
  • (C) Pre-defined containers and algorithms ✅
  • (D) None of these
  • Answer: (C).
  • Why: the STL has ready-made generic containers (vector, list, map, set), iterators and algorithms (sort, find). It has no GUI classes, and maths functions come from <cmath>, not the STL.