The Standard Template Library (STL) is one of the most powerful features of C++. It provides ready-made classes and functions that help developers write efficient, reusable, and high-performance programs without implementing common data structures and algorithms from scratch.
Instead of manually creating arrays, linked lists, stacks, or sorting algorithms, STL offers optimized implementations that are widely used in competitive programming, software development, and technical interviews. C++ Standard Template Library practice questions with solutions help to build concepts
The STL is divided into three major components:
- Containers
- Algorithms
- Iterators
What are Containers?
Containers are data structures that store collections of objects.
Common STL containers include:
- Vector
- List
- Queue
- Stack
- Deque
- Set
- Multiset
- Map
- Multimap
- Unordered Set
- Unordered Map
Example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers = {10, 20, 30};
for (int number : numbers)
{
cout << number << " ";
}
return 0;
}
Output
10 20 30
What are Algorithms?
Algorithms perform operations on containers.
Common STL algorithms include:
- sort()
- reverse()
- find()
- count()
- binary_search()
- max_element()
- min_element()
Example:
sort(numbers.begin(), numbers.end());
What are Iterators?
Iterators are objects used to traverse container elements.
Example:
for(vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++)
{
cout << *it;
}
Advantages of STL
- Faster Development
- Optimized Performance
- Code Reusability
- Generic Programming
- Industry Standard
- Easy to Learn
- Useful for Competitive Programming
STL is widely used in
- Software Development
- Competitive Programming
- Data Processing
- Banking Software
- E-commerce Applications
- Machine Learning Projects
- System Programming
In this chapter, you’ll solve beginner-friendly STL practice questions.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Output
- Explanation
- Concepts Covered
Let’s begin.
1. C++ Program to Store Elements Using Vector
Problem Statement
Write a C++ program to store and display elements using a vector.
C++ Solution
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers = {10, 20, 30, 40, 50};
for (int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
The vector stores multiple integers dynamically and allows easy traversal using a range-based loop.
Concepts Covered
- Vector
- Range-based Loop
- STL Container
2. C++ Program to Add Elements to a Vector
Problem Statement
Write a C++ program to insert elements into a vector using push_back().
C++ Solution
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
numbers.push_back(15);
for (int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
5 10 15
Explanation
The push_back() function inserts elements at the end of the vector.
Concepts Covered
- Vector
- push_back()
- Dynamic Storage
3. C++ Program to Find the Size of a Vector
Problem Statement
Write a C++ program to display the number of elements stored in a vector.
C++ Solution
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers = {2,4,6,8,10};
cout << "Size = "
<< numbers.size();
return 0;
}
Sample Output
Size = 5
Explanation
The size() function returns the total number of elements stored in the vector.
Concepts Covered
- Vector
- size()
- STL Functions
4. C++ Program to Sort a Vector
Problem Statement
Write a C++ program to sort elements of a vector in ascending order.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {25,10,30,5,20};
sort(numbers.begin(), numbers.end());
for(int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
5 10 20 25 30
Explanation
The sort() algorithm arranges vector elements in ascending order.
Concepts Covered
- sort()
- Algorithm Library
- Vector
5. C++ Program to Reverse a Vector
Problem Statement
Write a C++ program to reverse the elements of a vector.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {1,2,3,4,5};
reverse(numbers.begin(), numbers.end());
for(int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
5 4 3 2 1
Explanation
The reverse() algorithm reverses the order of elements in the vector.
Concepts Covered
- reverse()
- Algorithm
- Vector
6. C++ Program to Demonstrate Stack Using STL
Problem Statement
Write a C++ program to demonstrate stack operations using the Standard Template Library.
C++ Solution
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> numbers;
numbers.push(10);
numbers.push(20);
numbers.push(30);
cout << "Top Element = "
<< numbers.top();
return 0;
}
Sample Output
Top Element = 30
Explanation
A stack follows the LIFO (Last In, First Out) principle. The most recently inserted element is accessed first.
Concepts Covered
- Stack
- push()
- top()
- STL Container
7. C++ Program to Demonstrate Queue Using STL
Problem Statement
Write a C++ program to demonstrate queue operations using STL.
C++ Solution
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> numbers;
numbers.push(10);
numbers.push(20);
numbers.push(30);
cout << "Front Element = "
<< numbers.front() << endl;
cout << "Back Element = "
<< numbers.back();
return 0;
}
Sample Output
Front Element = 10
Back Element = 30
Explanation
A queue follows the FIFO (First In, First Out) principle. The first inserted element is removed first.
Concepts Covered
- Queue
- front()
- back()
- STL Queue
8. C++ Program to Demonstrate Set Using STL
Problem Statement
Write a C++ program to store unique elements using a set.
C++ Solution
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> numbers;
numbers.insert(40);
numbers.insert(10);
numbers.insert(30);
numbers.insert(20);
numbers.insert(20);
for(int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40
Explanation
A set automatically stores unique values in sorted order. Duplicate values are ignored.
Concepts Covered
- Set
- insert()
- Unique Elements
9. C++ Program to Demonstrate Map Using STL
Problem Statement
Write a C++ program to store key-value pairs using a map.
C++ Solution
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int,string> students;
students[101] = "Rahul";
students[102] = "Priya";
students[103] = "Amit";
for(auto student : students)
{
cout << student.first
<< " "
<< student.second
<< endl;
}
return 0;
}
Sample Output
101 Rahul
102 Priya
103 Amit
Explanation
A map stores data as key-value pairs and automatically sorts the keys in ascending order.
Concepts Covered
- Map
- Key-Value Pair
- Associative Container
10. C++ Program to Traverse a Vector Using Iterators
Problem Statement
Write a C++ program to traverse a vector using iterators.
C++ Solution
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers = {5,10,15,20};
vector<int>::iterator iterator;
for(iterator = numbers.begin();
iterator != numbers.end();
iterator++)
{
cout << *iterator << " ";
}
return 0;
}
Sample Output
5 10 15 20
Explanation
Iterators provide a standard way to traverse elements stored inside STL containers.
Concepts Covered
- Iterator
- begin()
- end()
- Vector Traversal
11. C++ Program to Find an Element in a Vector Using find()
Problem Statement
Write a C++ program to search for an element in a vector using the STL find() algorithm.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {10, 20, 30, 40, 50};
auto iterator = find(numbers.begin(), numbers.end(), 30);
if (iterator != numbers.end())
{
cout << "Element Found";
}
else
{
cout << "Element Not Found";
}
return 0;
}
Sample Output
Element Found
Explanation
The find() algorithm searches the container for the specified value. If found, it returns an iterator pointing to that element.
Concepts Covered
- find()
- Vector
- STL Algorithms
12. C++ Program to Find the Maximum Element in a Vector
Problem Statement
Write a C++ program to find the largest element in a vector.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {45, 20, 90, 15, 65};
cout << "Maximum Element = "
<< *max_element(numbers.begin(), numbers.end());
return 0;
}
Sample Output
Maximum Element = 90
Explanation
The max_element() algorithm returns an iterator pointing to the largest element in the container.
Concepts Covered
- max_element()
- STL Algorithms
- Vector
13. C++ Program to Find the Minimum Element in a Vector
Problem Statement
Write a C++ program to find the smallest element in a vector.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {45, 20, 90, 15, 65};
cout << "Minimum Element = "
<< *min_element(numbers.begin(), numbers.end());
return 0;
}
Sample Output
Minimum Element = 15
Explanation
The min_element() algorithm returns an iterator pointing to the smallest element in the vector.
Concepts Covered
- min_element()
- Vector
- STL Algorithms
14. C++ Program to Perform Binary Search Using STL
Problem Statement
Write a C++ program to perform binary search using the STL binary_search() function.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {10, 20, 30, 40, 50};
if (binary_search(numbers.begin(), numbers.end(), 30))
{
cout << "Element Found";
}
else
{
cout << "Element Not Found";
}
return 0;
}
Sample Output
Element Found
Explanation
The binary_search() function quickly checks whether an element exists in a sorted container.
Concepts Covered
- binary_search()
- STL Algorithms
- Searching
15. C++ Program to Count Occurrences of an Element Using STL
Problem Statement
Write a C++ program to count how many times a value appears in a vector.
C++ Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> numbers = {10, 20, 30, 20, 40, 20};
cout << "Occurrences of 20 = "
<< count(numbers.begin(), numbers.end(), 20);
return 0;
}
Sample Output
Occurrences of 20 = 3
Explanation
The count() algorithm counts the number of occurrences of a specified value within the container.
Concepts Covered
- count()
- STL Algorithms
- Vector
Chapter Summary
In this chapter, you learned the fundamentals of the C++ Standard Template Library (STL). You explored commonly used containers such as vector, stack, queue, set, and map, along with iterators for container traversal. You also practiced powerful STL algorithms including sort(), reverse(), find(), max_element(), min_element(), binary_search(), and count(). Mastering STL helps you write cleaner, faster, and more efficient C++ programs while reducing development time.
Key Takeaways
- STL stands for Standard Template Library.
- STL consists of containers, algorithms, and iterators.
vectoris a dynamic array that automatically manages memory.stackfollows the LIFO (Last In, First Out) principle.queuefollows the FIFO (First In, First Out) principle.setstores unique values in sorted order.mapstores data as key-value pairs with sorted keys.- Iterators are used to traverse STL containers.
- STL algorithms simplify common tasks such as searching, sorting, reversing, and counting.
- STL is heavily used in competitive programming, coding interviews, and real-world software development.
Frequently Asked Questions (FAQs)
1. What is the Standard Template Library (STL) in C++?
The Standard Template Library (STL) is a collection of pre-built classes and algorithms that provide efficient implementations of common data structures and operations.
2. What are the three main components of STL?
The three main components are:
- Containers
- Algorithms
- Iterators
3. What is a vector in STL?
A vector is a dynamic array that automatically resizes itself as elements are added or removed.
4. What is the difference between a stack and a queue?
- Stack: Last In, First Out (LIFO)
- Queue: First In, First Out (FIFO)
5. Why is set useful?
A set automatically stores unique elements in sorted order, making it useful when duplicate values should not be allowed.
6. What is the purpose of iterators?
Iterators provide a standard way to access and traverse elements stored in STL containers.
7. Which header file contains STL algorithms like sort() and find()?
The <algorithm> header file contains most STL algorithms.
8. Why is STL important in C++?
STL improves productivity by providing optimized, reusable implementations of data structures and algorithms. It is widely used in competitive programming, technical interviews, and professional software development.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
