C++ Standard Template Library (STL) - A Professional Guide

1. Standard Template Library (STL)

Q: What is the STL in C++?

The Standard Template Library (STL) is a powerful set of C++ template-based libraries providing generic containers, algorithms, iterators, and function objects. It enables efficient, reusable, and type-safe implementations of data structures and algorithms, making it a cornerstone for DSA tasks like sorting, searching, and graph processing.

Q: What are the main components of the STL?

Q: What are the types of STL containers?

Q: What are iterators and their types in the STL?

Iterators are objects that traverse containers, providing a uniform way to access elements. Types include:

Q: What are some common STL algorithms?

From <algorithm>:

Q: Can you give an example of using STL containers and algorithms?

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

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

    cout << "Original vector: ";
    for (int x : vec) cout << x << " ";
    cout << endl;

    sort(vec.begin(), vec.end());
    cout << "Sorted vector: ";
    for (int x : vec) cout << x << " ";
    cout << endl;

    auto it = find(vec.begin(), vec.end(), 5);
    if (it != vec.end())
        cout << "Found 5 at position: " << distance(vec.begin(), it) << endl;

    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    cout << "After removing duplicates: ";
    for (int x : vec) cout << x << " ";
    cout << endl;

    return 0;
}

Output:

Original vector: 5 2 9 1 5 6
Sorted vector: 1 2 5 5 6 9
Found 5 at position: 2
After removing duplicates: 1 2 5 6 9

Q: How is the STL used in DSA?

Q: Can you give a DSA-related example using the STL?

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

class Graph {
private:
    int vertices;
    vector<vector<int>> adjList;
public:
    Graph(int v) : vertices(v), adjList(v) {}

    void addEdge(int src, int dest) {
        adjList[src].push_back(dest);
        adjList[dest].push_back(src);
    }

    void bfs(int start) {
        vector<bool> visited(vertices, false);
        queue<int> q;
        visited[start] = true;
        q.push(start);
        cout << "BFS Traversal: ";
        while (!q.empty()) {
            int vertex = q.front(); q.pop();
            cout << vertex << " ";
            for (int neighbor : adjList[vertex]) {
                if (!visited[neighbor]) {
                    visited[neighbor] = true;
                    q.push(neighbor);
                }
            }
        }
        cout << endl;
    }

    bool hasEdge(int src, int dest) {
        auto it = find(adjList[src].begin(), adjList[src].end(), dest);
        return it != adjList[src].end();
    }
};

int main() {
    Graph g(4);
    g.addEdge(0, 1); g.addEdge(0, 2);
    g.addEdge(1, 2); g.addEdge(2, 3);
    g.bfs(0);
    cout << "Edge (1,2): " << (g.hasEdge(1,2) ? "Yes" : "No") << endl;
    cout << "Edge (1,3): " << (g.hasEdge(1,3) ? "Yes" : "No") << endl;
    return 0;
}

Output:

BFS Traversal: 0 1 2 3
Edge (1,2): Yes
Edge (1,3): No

Q: How does the STL differ from C?

Q: What are common mistakes with the STL?

Q: What are best practices for using the STL in C++?

Q: How do functors work in the STL?

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

struct CompareDescending {
    bool operator()(int a, int b) const {
        return a > b;
    }
};

int main() {
    vector<int> vec = {5, 2, 9, 1, 5, 6};
    sort(vec.begin(), vec.end(), CompareDescending());
    cout << "Sorted (descending): ";
    for (int x : vec) cout << x << " ";
    cout << endl;
    return 0;
}

Output:

Sorted (descending): 9 6 5 5 2 1