C++ STL (Standard Template Library) Interview Questions (original) (raw)

Last Updated : 26 Aug, 2025

In C++, the Standard Template Library (STL) provides ready-made tools for handling data structures and algorithms efficiently. Built on templates, STL works with any data type and includes four main parts: containers, algorithms, iterators, and function objects.

Below are some of the most important interview questions about STL.

1. What is STL?

STL stands for Standard Template Library. It provides four main components:

  1. Containers (data structures like vector, list, map, etc.)
  2. Algorithms (sorting, searching, etc.)
  3. Iterators (generalized pointers for traversal)
  4. Function objects (functors used with algorithms)

2. What is the difference between an array and a list?

The major differences between the arrays and lists are:

Arrays Lists
Array are contiguous memory, has fixed size, allows O(1) random access, efficient memory use. Lists are classic individual elements that are linked or connected to each other with the help of pointers and do not have a fixed size.
Arrays are static in nature. Lists are dynamic in nature
Uses less memory than linked lists. Uses more memory as it has to store the value and the pointer memory location

3. What are STL containers? Explain different types with examples.

STL containers are predefined data structures in C++ that store collections of data in various ways. They are categorized into three types:

**1. Sequence Containers: Maintain the order of insertion.

**Example:

vector v = {1, 2, 3};

**2. Associative Containers: Store elements using keys (ordered).

**Example:

map<string, int> age;
age["Alice"] = 25;

**3. Unordered Associative Containers: Use hash tables instead of trees.

**Example:

unordered_map<string, int> scores;

**4. Container Adapters: Built on other containers with restricted interface

Containers save time and effort by providing built-in operations like insertion, deletion, and search with optimal complexity.

4. How is the vector container implemented in STL? Explain its Features and limitations.

The vector container in C++ STL is a dynamic array that can grow or shrink in size at runtime. It provides random access to elements and stores them in contiguous memory locations, like traditional arrays.

**Implementation Details:

**Features:

**Limitations:

**Example:

C++ `

#include #include using namespace std;

int main() { vector v = {10, 20, 30}; v.push_back(40); // add at end v.pop_back(); // remove last cout << v[1]; // random access }

`

The vector is one of the most commonly used STL containers due to its efficiency, flexibility, and support for standard operations.

5. What happens if you modify a vector while iterating through it using a range-based for loop?

If you **modify a std::vector while iterating through it using a range-based for loop, the behavior can be unpredictable and often unsafe. Here's why:

**Wrong Usage:

C++ `

#include #include<bits/stdc++.h> using namespace std;

int main() {

vector<int> v = {1, 2, 3, 4};
for (int x : v) {
    if (x == 2)
        v.push_back(5); // Modifying vector during iteration
}
return 0;

}

`

**Correct Approach (Using Index):

C++ `

#include #include<bits/stdc++.h> using namespace std;

int main() { vector v = {1, 2, 3, 4}; for (size_t i = 0; i < v.size(); ++i) { if (v[i] == 2) v.push_back(5); // Safe as size is checked } return 0; }

`

One assume the range-based loop reflects vector changes, but it doesn’t, it can crash or skip elements

6. What are iterators in STL? Describe types of iterators with examples.

An iterator is an object (like a pointer) used to traverse containers. STL uses iterators to access elements in a uniform manner, regardless of the container. Iterators help in writing generic algorithms that work across different container types.

**Types of Iterators:

Type Description
Input Iterator Read-only access, single pass
Output Iterator Write-only access, single pass
Forward Iterator Read/write, multiple passes
Bidirectional Traverse both directions
Random Access Direct jump to any position (e.g., vector)

**Example:

C++ `

#include #include using namespace std;

int main() {

vector<int> v = {10, 20, 30};
vector<int>::iterator it;

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

return 0;

}

`

7. What is the effect of modifying a container (e.g., inserting or erasing elements) during iteration? How can you safely erase elements from a vector or set while iterating?

Modifying a container (like inserting or erasing elements) while iterating over it can lead to undefined behavior because iterators can become invalidated.

To safely erase elements during iteration, we must:

**Example Code:

C++ `

#include #include #include using namespace std;

int main() { vector vec = {1, 2, 3, 4, 5, 3, 6};

// Remove all 3s safely
vec.erase(remove(vec.begin(), vec.end(), 3), vec.end());

for (int x : vec)
    cout << x << " ";
return 0;

}

`

remove() shifts all non-3s forward and returns a new end iterator. erase() then removes the trailing garbage efficiently.

5. What are function objects (functors) in STL? How are they used?

**How are they used?

**Example:

C++ `

#include <bits/stdc++.h> using namespace std;

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

int main() { vector v={3,2,65,34,98,45}; sort(v.begin(), v.end(), Compare()); // Custom sorting for(int i:v) cout<<i<<" "; return 0; }

`

**Function objects:

They provide flexibility in customizing STL operations like sorting or filtering.

6. What are lambda expressions in C++? How do they relate to STL?

Lambda expressions are anonymous functions defined inline using the [] syntax. They are commonly used with STL algorithms to simplify code.

**Syntax:

[ capture ] (parameters) -> return_type { body }

**Example:

C++ `

#include <bits/stdc++.h> #include using namespace std;

int main() {

vector<int> v = {1, 2, 3, 4, 5};

int count = count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
cout<<count;
return 0;

}

`

**Advantages:

Lambdas improve clarity and reduce boilerplate in functional-style C++ code.

7. What are allocators in STL? Can they be customized?

Allocators define how memory is allocated and deallocated for STL containers. By default, containers use std::allocator which uses new/delete. Custom allocators can be provided for special needs such as memory pools, shared memory, or tracking.

**Custom Allocator Example:

C++ `

template class MyAllocator { // Implement custom allocate, deallocate, etc. }; vector<int, MyAllocator> customVec;

`

**Use Cases:

8. Explain the difference between map, unordered_map, and multimap in STL.

Feature map unordered_map multimap
Ordering Ordered by keys (ascending by default) No ordering (hash table based) Ordered by keys (allows duplicates)
Underlying structure Red-Black Tree (Balanced BST) Hash Table Red-Black Tree (like map)
Key Uniqueness Keys are unique Keys are unique Allows duplicate keys
Search Time O(log n) search time Average O(1), worst O(n) O(log n) search time
Insertion Time O(log n) insertion time Average O(1), worst O(n) O(log n) insertion time
Duplicates Allowed No duplicates allowed No duplicates allowed Duplicates allowed
Header File header file <unordered_map> header file header file
Use Case Use when sorted keys are needed Use for fast access without ordering Use when multiple values per key are needed

**Example:

map<int, string> m;
unordered_map<int, string> um;
multimap<int, string> mm;

Choosing the right container depends on whether order, speed, or duplicate keys are required.

12. What happens if you insert a duplicate key in std::set or std::map? How do you detect insertion success?

Set and map do not allow duplicate keys. The insert() method returns a pair<iterator, bool>, where bool indicates success.

**Example:

C++ `

#include #include<bits/stdc++.h> using namespace std;

int main() {

set<int> s;
auto [it, success] = s.insert(10);
cout << (success ? "Inserted" : "Duplicate") << endl;

tie(it, success) = s.insert(10); // Try duplicate
cout << (success ? "Inserted" : "Duplicate") << endl;

return 0;

}

`

One often assume insert() silently overwrites in set, but it doesn’t. Checking return value is key.

13. What is the difference between emplace and insert in STL containers? When is emplace preferred?

insert emplace
Constructs the object before insertion Constructs the object in-place inside the container
Requires a fully constructed object as an argument Accepts constructor arguments directly
May involve copy or move operations Avoids unnecessary copy/move; more efficient

**Example:

C++ `

#include #include using namespace std;

struct A { int x; A(int x) : x(x) { cout << "Constructed with " << x << endl; }

bool operator<(const A& other) const {
    return x < other.x;
}

};

int main() { set s; s.emplace(5); s.insert(A(10)); }

`

Output

Constructed with 5 Constructed with 10

emplace() can significantly improve performance in complex objects or in associative containers like map.

14. Why does std::remove not actually remove elements from a vector? What is the remove-erase idiom?

**Remove-Erase Idiom:

v.erase(std::remove(v.begin(), v.end(), value), v.end());

**Example:

C++ `

#include #include<bits/stdc++.h> using namespace std;

int main() {

vector<int> v = {1, 2, 3, 2, 4};
v.erase(remove(v.begin(), v.end(), 2), v.end());
for(auto it:v) cout<<it<<" ";
return 0;

}

`

Beginners expect remove() to delete from the container, but STL separates algorithm logic (remove) from container mutation (erase).

15. How do you sort a vector<pair<int, int>> by second element using STL?

To sort a vector<pair<int, int>> by the second element of the pairs using STL, you can use std::sort with a custom comparator.

**Example:

C++ `

#include #include<bits/stdc++.h> using namespace std;

int main() {

vector<pair<int, int>> v = {{1, 4}, {2, 3}, {3, 2}};
sort(v.begin(), v.end(), [](auto &a, auto &b) {
    return a.second < b.second;
});
for(auto& [key,val]:v) cout<<key<<" : "<<val<<endl;
return 0;

}

`

STL sort by default sorts by .first for pairs.

16. What happens if you use a vector as a key in an unordered_map?

Using a std::vector as a key in an unordered_map is **not allowed by default because:

To use std::vector as a key in unordered_map, you must provide:

  1. A custom hash function for the vector.
  2. An equality comparator (if needed, default operator== works for vectors).

**Fix with Custom Hasher:

C++ `

#include #include #include using namespace std;

struct VectorHash { size_t operator()(const vector& v) const { size_t h = 0; for (int x : v) h ^= hash()(x) + 0x9e3779b9 + (h << 6) + (h >> 2); return h; } };

int main() { unordered_map<vector, int, VectorHash> m; m[{1, 2, 3}] = 10; cout << m[{1, 2, 3}]; }

`

Note: Most STL types like vector work in map, but not in unordered_map unless hash function is provided.