The C++ Standard Template Library (STL) provides production-grade implementations of the data structures and algorithms that appear repeatedly in ML systems code.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/VrajPatel105/cpp-gpu-inference/llms.txt
Use this file to discover all available pages before exploring further.
std::vector replaces manually managed dynamic arrays, std::unordered_map provides O(1) average-case key-value lookup, and the <algorithm> header delivers sorting, filtering, and counting without writing any loops by hand. Lambdas tie these together by letting you define short comparison or predicate functions inline, exactly where they are used. The stl.cpp module in this project exercises all four in a single cohesive task: read ten integers, sort descending, count evens, and build a frequency map.
std::vector: Dynamic Contiguous Arrays
std::vector<T> is a heap-allocated array that grows automatically. Elements are stored contiguously in memory, so indexing is O(1) and cache performance matches a plain C array.
push_back
Appends an element to the end. Amortized O(1) — the vector occasionally
reallocates to a larger buffer, but not on every call.
reserve(n)
Pre-allocates capacity for
n elements without changing size. Use this
before filling a vector in a loop to avoid repeated reallocations.std::unordered_map: Hash Map Lookup
std::unordered_map<K, V> stores key-value pairs with O(1) average-case insert and lookup. The mpp[key]++ idiom is idiomatic for building frequency tables: if the key does not exist, it is default-constructed (to 0 for int) before being incremented.
unordered_map iteration order is unspecified — the pairs come back in hash
bucket order, not insertion order. Use std::map (a red-black tree) if
sorted iteration is required, at the cost of O(log n) operations.<algorithm>: Sort and Count
The <algorithm> header provides generic algorithms that operate on iterator ranges. Passing begin() and end() of a vector covers the entire container.
Sorting with a Lambda Comparator
std::sort accepts an optional third argument: a binary comparator that returns true if the first argument should come before the second. A lambda defined inline avoids the need for a separate named function.
Counting with a Predicate
std::count_if counts elements that satisfy a predicate lambda:
Lambdas
A lambda is an anonymous function object defined inline. The general syntax is:- Sort comparator
- count_if predicate
- Capturing local variables
The Full STL Task in One Program
Thestl.cpp module chains all four tools together: