STL in C++
STL in C++
Objective
• STL Introduction
• STL Classes
STL(Standard Template Library)
Standard Template Library in C++ is a powerful set of C++ template classes to
provides general-purpose classes and functions that implement many popular and
commonly used algorithms and data structures.
The container manages the storage space for its elements and provides member
functions to access them, either directly or through iterators.
At the core of the standard template library there are three foundational items:
containers,algorithms, and iterators.
Container class templates
Sequence Containers: vector, deque, list
Just like arrays, vectors use contiguous storage locations for their elements, But unlike
arrays, their size can change dynamically, with their storage being handled automatically by
the container.
Therefore, compared to arrays, vectors consume more memory in exchange for the ability
to manage storage and grow dynamically in an efficient way.
Elements in Vector containers are ordered in a strict linear sequence. Individual elements
are accessed by their position in this sequence.
Vector Member Functions
Member Function Description
constructor() Construct vector
iterartor begin() Return iterator to beginning
iterator end() Return iterator to end
rbegin() Return reverse_iterator to reverse beginning
rend() Return reverse_iterator to reverse end
size_type size() Return size
size_type max_size() Return maximum size
size_type capacity() Return size of allocated storage capacity
bool empty() Test whether vector is empty
void resize(size,val) Change size
void reserve(capacity) Request a change in capacity
rerefence operator[], Access element at specified index
reference at()
reference front(), Access first and last element respectively
reference back()
Vector Member Functions
Member Function Description
void assign(first,last), Assign vector content
void assign(n,value)
void push_back(const T &val) Add element at the end
void pop_back() Delete last element
iterator insert(iterator i,value), Insert element
void insert(iterator i,num,value) Insert elements
iterator insert(iterator i,InIter Insert element
start,InIter end)
iterator erase(iterator i), Erase elements
iterator erase(start,end)
void clear() Clear content, leaving the container with size of 0.
void swap(vector &) Swap content
list template class
template < class T, class Alloc = allocator<T> > class list;
Lists are sequence containers that allow constant time insert and erase operations
anywhere within the sequence, and iteration in both directions.
They are implemented as doubly-linked lists
The main drawback of lists compared to other sequence containers is that they lack
direct access to the elements by their position.
Elements in List containers are also ordered in a strict linear sequence. Individual
elements are accessed by their position in this sequence.
Each element keeps information on how to locate the next and the previous elements.
list Member Functions
Member Function Description
constructor() Construct list
iterartor begin() Return iterator to beginning
iterator end() Return iterator to end
rbegin() Return reverse_iterator to reverse beginning
rend() Return reverse_iterator to reverse end
size_type size() Return size
size_type max_size() Return maximum size
bool empty() Test whether list is empty
void resize(size,val) Change size
reference front(), Access first and last element respectively
reference back()
void assign(first,last), Assign new list content
void assign(n,value)
void push_back(const T &val) Add element at the end
Maps are associative containers that store elements formed by a combination of a key
value and a mapped value, following a specific order.
In a map, the key values are generally used to sort and uniquely identify the elements,
while the mapped values store the content associated to this key.
The types of key and mapped value may differ, and are grouped together in member type
value_type, which is a pair type combining both:
iterator lower_bound (const Returns an iterator pointing to the first element in the container
key_type& k) whose key is not considered to go before k
iterator upper_bound (const Returns an iterator pointing to the first element in the container
key_type& k); whose key is considered to go after k.
map Member Functions
Member Function Description
pair<iterator, bool> Inserts val into the invoking map. An iterator to the element is
insert(const value_type &val); returned. The element is inserted only if it does not already
exist.
void insert(iterator i,value) Insert elements
iterator insert(InIter start,InIter end) Insert element
By default it uses a deque as a container, but a stack can only be accessed in a last-
in, first-out manner. You may also use a vector or list as a container for a stack.
stack Member Functions
Member Function Description
constructor() Construct stack
size_type size() Returns number of elements in Stack
bool empty() Test whether stack is empty
reference top() Returns a reference to the top of the stack, which is the
last element in the container. The element is not
removed.
void push(const T &val) Insert a new element at Top of Stack
Priority queues are a type of container adaptors, specifically designed such that its
first element is always the greatest of the elements it contains.
The standard container classes vector and deque fulfill these requirements. By
default, if no container class is specified for a particular priority_queue class
instantiation, the standard container vector is used.
priority_queue Member Functions
Member Function Description
constructor() Construct priority queue
size_type size() Returns number of elements in priority queue
bool empty() Test whether container is empty
reference top() Returns a reference to the first element in the priority
queue.
void push(const T &val) Insert a new element at the end of priority queue