Introduction to ArtificiaI Intelligence in Higher Education
C++ STL
1. C++ STL with Problem Solving
Google Developer Students Club
Indian Institute of InformationTechnology, Kota
2. Introduction to STL
• The StandardTemplate Library (STL) is a set of C++ template
classes to provide common programming data structures and
functions such as lists, stacks, arrays, etc.
• It is a library consisting of 4 components:
1.Algorithms
2.Containers
3.Functions
4.Iterators
3. Vector
Vectors are the same as dynamic arrays with the ability to resize
itself automatically when an element is inserted or deleted, with
their storage being handled automatically by the container.
Syntax : vector < datatype > name initial size ;
Methods :
• push_back(val), insert(index, val), pop_back(), erase(position)
• begin(), end(), rbegin(), rend()
• size(), empty()
4. Pairs
Pair is used to combine together two values that may be different in
type.The first element is referenced as ‘first’ and the second
element as ‘second’ and the order is fixed (first, second).
Syntax : pair < datatype, datatype > name;
Methods :
• make_pair(value_1, value_2)
• first, second - (accessing elements)
• swap(another_pair)
5. Iterators
Iterators are used to point at the memory addresses of STL
containers.They are primarily used in sequences of numbers,
characters etc.
Syntax : container<datatype>::iterator name;
Methods :
• begin(), end(), rbegin(), rend()
• advance(itr, steps)
• next(itr, steps), prev(itr, steps) – returns iterator
6. Stack
Stacks are a type of container adaptors with LIFO(Last In First Out)
type of working, where a new element is added at one end (top) and
an element is removed from that end only.
Syntax : stack < datatype > name;
Methods :
• push(val), pop()
• top() - (accessing elements)
• size(), empty()
7. Queue
Queues are a type of container adaptors that operate in a first in
first out (FIFO) type of arrangement. Elements are inserted at the
back (end) and are deleted from the front.
Syntax : queue < datatype > name;
Methods :
• push(val), pop()
• front(), back() - (accessing elements)
• size(), empty()
8. Priority Queue / Heap
Priority queues are designed such that the first element of the queue is either the
greatest or the smallest of all elements in the queue.
Syntax :
priority_queue < datatype > name; (max heap)
priority_queue < datatype, vector < datatype >, greater < datatype >> name;
(min heap)
Methods :
• push(val), pop()
• top() - (accessing elements)
• size(), empty()
9. Set / Unordered Set
Sets are a type of associative container in which each element has to be unique
because the value of the element identifies it.The values are stored in a specific
sorted order.
Syntax : set < datatype > name;
Methods :
• insert(), erase()
• begin(), end(), rbegin(), rend()
• size(), empty()
10. Map / Unordered Map
Maps are associative containers that store elements in a mapped fashion. Each
element has a key value and a mapped value. No two mapped values can have the
same key values.
Syntax : map < datatype, datatype > name;
Methods :
• insert(pair<int,int>(key,val))
• erase() – (value/iterator)
• begin(), end(), rbegin(), rend()
• size(), empty()
11. Algorithm Library
STL has a large number of inbuilt functions which are available in the algorithm
library.
Some important and frequently used functions are :
• sort(first_iterator, last_iterator);
• reverse(first_iterator, last_iterator);
• *max_element(first_iterator, last_iterator);
• *min_element(first_iterator, last_iterator);
• accumulate(first_iterator, last_iterator, initial_sum);
• count(first_iterator, last_iterator, element);
• find(first_iterator, last_iterator, element);
12. Algorithm Library
Additional functions:
• fill_n(start_iterator, range, value);
• lower_bound(start_iterator, end_iterator, value);
• upper_bound(start_iterator, end_iterator, value);
• is_sorted(start_iterator, end_iterator);
For the list of all STL algorithm library functions, visit the link below.
https://cplusplus.com/reference/algorithm/