Courses/C++/Standard Template Library

    Lesson 12 • Expert

    Standard Template Library

    Master vectors, maps, sets, queues, and algorithms — the powerful toolkit that makes C++ development fast and efficient.

    What You'll Learn

    • Vectors and maps for data storage
    • Sets, queues, stacks, and priority queues
    • STL algorithms (sort, find, transform)
    • Practical patterns: word counting, deduplication

    The STL at a Glance

    The STL is a collection of template-based containers and algorithms that ship with every C++ compiler. It's like having a professional toolbox — you don't build a hammer from scratch, you reach for the right tool.

    ContainerTypeAnalogyBest For
    vectorSequenceResizable arrayMost general use
    mapAssociativeDictionaryKey-value lookups
    setAssociativeUnique collectionNo duplicates, fast search
    queueAdapterLine at a storeFIFO processing
    stackAdapterStack of platesLIFO processing
    priority_queueAdapterER triageLargest/smallest first

    Vectors & Maps

    Store, iterate, insert, and erase with vectors and maps

    Try it Yourself »
    C++
    #include <iostream>
    #include <vector>
    #include <map>
    #include <string>
    using namespace std;
    
    int main() {
        // === VECTOR ===
        cout << "=== Vector ===" << endl;
        vector<string> fruits = {"Apple", "Banana", "Cherry"};
        fruits.push_back("Date");
        fruits.push_back("Elderberry");
        
        // Iterate
        for (const string &f : fruits) {
            cout << "- " << f << endl;
        }
        cout << "Size: " << fruits.size() << endl;
        
        // Insert and erase
        fruits.insert(fruits.begin() + 
    ...

    Sets, Queues & Stacks

    Use unique sets, FIFO queues, LIFO stacks, and priority queues

    Try it Yourself »
    C++
    #include <iostream>
    #include <set>
    #include <queue>
    #include <stack>
    #include <string>
    using namespace std;
    
    int main() {
        // === SET — unique, sorted elements ===
        cout << "=== Set ===" << endl;
        set<int> numbers = {5, 3, 8, 1, 3, 5, 8};  // Duplicates removed!
        numbers.insert(4);
        numbers.insert(1);  // Already exists, ignored
        
        for (int n : numbers) {
            cout << n << " ";  // Sorted: 1 3 4 5 8
        }
        cout << endl;
        
        // Check membership
        cout << "Contain
    ...

    STL Algorithms

    Sort, search, transform, count, and deduplicate with algorithms

    Try it Yourself »
    C++
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    
    int main() {
        vector<int> nums = {42, 17, 93, 5, 68, 31, 85, 12};
        
        // Sort
        sort(nums.begin(), nums.end());
        cout << "Sorted: ";
        for (int n : nums) cout << n << " ";
        cout << endl;
        
        // Sort descending
        sort(nums.begin(), nums.end(), greater<int>());
        cout << "Descending: ";
        for (int n : nums) cout << n << " ";
        cout << endl;
        
        // Find
        sort(nu
    ...

    Practical: Word Counter

    Build a word frequency counter and sort by occurrence

    Try it Yourself »
    C++
    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    int main() {
        // Practical: Word frequency counter
        string text = "the cat sat on the mat the cat likes the mat";
        
        map<string, int> wordCount;
        istringstream stream(text);
        string word;
        
        while (stream >> word) {
            wordCount[word]++;
        }
        
        cout << "=== Word Frequencies ===" << endl;
        for (const auto &[w, count] : wordCou
    ...

    Common Mistakes

    ⚠️ Iterator invalidation: Modifying a container (insert/erase) while iterating can crash. Use the return value of erase() to continue safely.

    ⚠️ Using [] on map creates entries: myMap["missing"] creates a default entry. Use .count() or .find() to check existence.

    ⚠️ Forgetting sort before binary_search: binary_search requires sorted data — unsorted data gives wrong results.

    Pro Tips

    💡 Use unordered_map for speed: unordered_map uses hashing for O(1) average lookups vs O(log n) for map.

    💡 Structured bindings: for (auto &[key, val] : myMap) is the modern way to iterate maps (C++17).

    💡 Lambda functions: Use lambdas with STL algorithms: sort(v.begin(), v.end(), [](int a, int b) { return a > b; });

    Lesson Complete!

    You now have a solid command of the STL — the most important library in C++. Next up: Memory Management — master dynamic allocation with new, delete, and smart pointers.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service