CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Cyber Safety and Ethics · 8 min · free

The Standard Template Library: vector, set, map and Algorithms

8 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

The Standard Template Library gives ready-made, tested containers, iterators and algorithms that work for any type.

Follows the syllabus of: CBSE Class 9 Computer Applications (165), ISC Class 11 Computer Science (868), GSEB Std 10 Computer Studies, Cambridge IGCSE Grade 10 Computer Science (0478)

On screen in this lesson

Why the STL exists

Most programs store, search and sort data
Writing this again and again wastes time
The STL gives ready-made, tested tools
It is built with templates, so it works for any type
It is part of standard C++, so it works everywhere

Three parts of the STL

PartWhat it isExamples
Containershold the datavector, set, map
Iteratorspoint to itemsbegin(), end()
Algorithmswork on datasort, find, count

Useful vector functions

push_back(x) adds x at the end
pop_back() removes the last item
size() tells how many items there are
v[i] reads item i; v.at(i) also checks the index
A vector grows by itself, unlike an array

How iterators work

begin() points to the first item
end() points one step past the last item
*it gives the value at that position
++it moves to the next item
auto lets the compiler pick the iterator type

set, map or pair?

ToolHoldsUse it for
setunique valuesroll numbers
mapkey and valuename to price
pairexactly twoname and marks

Quick recap

The STL gives tested containers and algorithms
vector grows; iterators begin() and end() walk it
set is unique and sorted; map links key to value
pair holds two values: first and second
sort, find, count; binary_search needs sorted data

Quick answers

set {30, 10, 30, 20}: what is the size?

3. Repeats are dropped and values are sorted.

What does binary_search need?

Sorted data.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Would you write your own sorting code for every program? Good programmers do not. C plus plus comes with a ready made toolbox called the Standard Template Library. Today we learn why it exists, vector and iterators, set, map and pair, and four famous algorithms: sort, find, count and binary search.

Why does the STL, the Standard Template Library, exist? Most programs store data, search it and sort it. Writing that code again in every program wastes time and invites bugs. The STL gives ready made tools, already tested by experts and fast. It is built with templates, so the same tool works for int, string or any type. And it is part of standard C plus plus, so it works with every modern compiler.

The STL has three main parts. Containers are objects that hold data, like vector, set and map. Iterators are like pointers that move through a container, using begin and end. Algorithms are ready made functions that work on the data, like sort, find and count.

A vector is like an array that can grow. To use it, include the header vector. Here runs is a vector of int holding two cricket scores, forty five and twelve. Push back adds seventy eight at the end, so the size becomes three. Runs at index two is seventy eight. So the output is three and seventy eight.

Here are the vector functions you will use most. Push back adds a value at the end. Pop back removes the last value. Size tells how many items the vector holds. Square brackets read an item by index, and the at function does the same but also checks that the index is valid. Best of all, a vector grows by itself, while a normal array has a fixed size.

An iterator is an object that points to one item in a container, like a finger moving along a list. Here the vector named m holds three marks. The loop starts at m dot begin, which points to the first item. It keeps going while the iterator is not equal to m dot end. Star i gives the value it points to, and i plus plus moves to the next item. So it prints seventy two, eighty five and sixty four.

Let us make iterators clear. Begin points to the first item. End points one step past the last item, not to the last item itself, so never read star end. Star gives the value at the current position. Plus plus moves the iterator to the next item. The keyword auto lets the compiler work out the long iterator type for you.

A set stores each value only once, and always keeps values in sorted order. Pause and predict. We put in thirty, ten, thirty and twenty. What is the size? The repeated thirty is dropped, and the rest are sorted. So it prints ten, twenty, thirty, and the size is three.

A pair joins two values, which can be of different types, into one object. Here the pair named p holds the name Riya and her marks, ninety two. The first value is reached with dot first. The second value is reached with dot second. So it prints Riya ninety two.

A map stores key and value pairs, like a canteen price list. The key is the item name, and the value is its price. Fee of Tea is ten rupees, and fee of Samosa is fifteen. Each entry inside a map is a pair. A map keeps its keys sorted, so Samosa comes before Tea. Finally, fee of Tea looks up the price, ten.

How do you choose? A set holds unique values, which suits a list of roll numbers with no repeats. A map links a key to a value, which suits an item name and its price. A pair holds exactly two values, which suits a student name with marks.

Now the algorithms, which need the header algorithm. Sort arranges items in ascending order. We pass two iterators, v dot begin and v dot end, to say sort the whole vector. The marks sixty four, twelve, eighty five and twelve become twelve, twelve, sixty four, eighty five.

Count tells how many times a value appears between two iterators. Pause and predict. How many times does twelve appear? Two times. So count prints two.

Find searches for a value and returns an iterator to its first match. Here it finds eighty five. Subtracting v dot begin from that iterator gives the index, which is two. If the value is not there, find returns v dot end, so always compare with end before using it.

Binary search only answers yes or no. It is found, or it is not. The data must already be sorted, or the answer can be wrong. Here the vector is sorted, and sixty four is present, so it prints one, meaning true. Searching for fifty would print zero. It is much faster than find on large sorted data, because it halves the search each step.

Some exams ask you to write the search yourself in pseudocode. This is a linear search, the same idea find uses. We set Found to false. The loop checks each mark from position one to four. If a mark equals eighty five, Found becomes true. At the end we output Found. Exam arrays in pseudocode often start at one, while C plus plus starts at zero.

Let us revise what we learned today. The STL gives ready made, tested containers and algorithms. A vector grows by itself, and iterators from begin to end walk through it. A set keeps unique values in sorted order, and a map links a key to a value. A pair holds two values, called first and second. Sort, find and count work on any range, but binary search needs sorted data. Try storing your class marks in a vector and sorting them.

Courses that teach this

CourseUnit
CBSE Class 9 Computer Applications (165)Office tools
ISC Class 11 Computer Science (868)Software and Algorithmic Problem Solving
GSEB Std 10 Computer StudiesProgramming Fundamentals & C Language
Cambridge IGCSE Grade 10 Computer Science (0478)7. Algorithm Design and Problem-Solving
Edexcel GCSE GCSE Computer Science (1CP2)Topic 1: Computational thinking
Programming All levels C++STL and Data Structure Fundamentals

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →