0% found this document useful (0 votes)
86 views24 pages

STL in C++

The document discusses the Standard Template Library (STL) in C++. It introduces common STL containers like vector, deque, list, stack, queue, priority_queue, set, map, multiset, multimap. It describes various member functions of these containers and how to use iterators to traverse container elements. The document also provides examples of storing objects in STL containers like map.

Uploaded by

Ram Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views24 pages

STL in C++

The document discusses the Standard Template Library (STL) in C++. It introduces common STL containers like vector, deque, list, stack, queue, priority_queue, set, map, multiset, multimap. It describes various member functions of these containers and how to use iterators to traverse container elements. The document also provides examples of storing objects in STL containers like map.

Uploaded by

Ram Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

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.

 Such classes are also called as Standard Containers.

 A container is a holder object that stores a collection of other objects (its


elements).

 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

 Container Adaptors: stack, queue, priority_queue

 Associative Containers: set, map, multiset, multimap


Iterators
 Iterators are objects and are handled just like pointers. You can increment and
decrement them. You can apply the * operator to them. Iterators are declared using
the iterator type defined by the various containers.
 There are five types of iterators:
1. Random Access: Store and retrieve values. Elements may be accessed
randomly.
2. Bidirectional: Store and retrieve values. Forward and backward moving.
3. Forward :Store and retrieve values. Forward moving only.
4. Input :Retrieve, but not store values. Forward moving only.
5. Output: Store, but not retrieve values. Forward moving only.
vector template class
template < class T, class Alloc = allocator<T> > class vector;

Vectors are sequence containers representing arrays that can change in


size.

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

void push_front(const T &val) Insert Element At Beginning


list Member Functions
Member Function Description
void pop_back() Delete last element
void pop_front() Delete first 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(list &) Swap content
void merge(list<T, Allocator> ob) Merges the ordered list contained in ob with the ordered
invoking list. The result is ordered. After the merge, the list
contained in ob is empty.
void remove(const T &val) Removes elements with the value val from the list.
void reverse( ) Reverses the invoking list.
void sort( ) Sorts the list.
void splice(iterator i,list &ob) , The contents of ob are inserted into the invoking list at the
void splice(iterator i,list &ob,iterator location pointed to by i. After the operation, ob is empty.
el) ,
map template class
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;

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:

typedef pair<const Key, T> value_type;


map Member Functions
Member Function Description
constructor() Construct map
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
rerefence operator[] Access element at specified key
iterator find (const key_type& k) Returns an iterator to the specified key. If the key is not found,
then an iterator to the end of the map is returned.
size_type count(const key_type &k) Returns the number of times k occurs in the map (1 or zero).

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

iterator erase(iterator i), Erase elements


iterator erase(start,end),
size_type erase(key_type &k)
void clear() Clear content, leaving the container with size of 0.
void swap(map &) Swap content
Storing Object in map
class Student {
string name;
public:
Student()
{}
Student(string name)
{
this->name=name;
}
string get()
{
return name;
}
};
int operator<(Student s1,Student s2)
{
return s1.get().compare(s2.get())<0;
}
deque template class
template < class T, class Alloc = allocator<T> > class deque;

deque is an acronym of double-ended queue. Double-ended queues are sequence


containers with dynamic sizes that can be expanded on both ends.
deque Member Functions
Member Function Description
constructor() Construct deque
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
rerefence operator[], reference at() Access element at specified index
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

void push_front(const T &val) Insert Element At Beginning


deque Member Functions
Member Function Description
void pop_back() Delete last element
void pop_front() Delete first 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(deque &) Swap content
stack template class
template <class T, class Container = deque<T> > class stack;

Stacks are a type of container adaptor, specifically designed to operate in a LIFO


context (last-in first-out), where elements are inserted and extracted only from one
end of the container.

 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

void pop() Removes the element on top of the stack, effectively


reducing its size by one.
queue template class
template <class T, class Container = deque<T> > class queue;

queues are a type of container adaptor, specifically designed to operate in a FIFO


context (first-in first-out), where elements are inserted into one end of the container
and extracted from the other.
By default it uses a deque as a container, but a queue can only be accessed in a
first-in, first-out manner. You can also use a list as a container for a queue.
queue Member Functions
Member Function Description
constructor() Construct queue
size_type size() Returns number of elements in queue
bool empty() Test whether queue is empty
reference back() Returns a reference to the last element in the queue.

reference front() Returns a reference to the first element in the queue.

void push(const T &val) Insert a new element at the end of queue

void pop() Removes the first element in the queue, effectively


reducing its size by one.
priority_queue template class
template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;

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

void pop() Removes the first element in the priority queue,


effectively reducing its size by one.

You might also like