OOPs With C - CSE2001 - Unit 4 - STL
OOPs With C - CSE2001 - Unit 4 - STL
By:
Dr. Ramraj Dangi
● Introduction to object oriented
approach
● Classes and objects
Chapters ● Polymorphism and Inheritance
● Exception handling and Templates
● IOstreams and Files
Exception handling (user-defined
exception),
The Standard Template 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 of container classes, algorithms, and iterators. It is a generalized library and so, its
components are parameterized.
Components of STL;
● Algorithms
● Containers
● Iterators
Containers
● Container library in STL provide containers that are used to create data structures like arrays
linked list, trees etc.
● These container are generic, they can hold elements of any types.
● There are in total seven standard “first-class” container classes and three container adaptor
classes and only seven header files that provide access to these containers or container
adaptors.
Algorithms
● Algorithms can act on containers. They provide the means by which you will perform
initialization, sorting , searching, and transforming of the contents of containers.
● Algorithms library contains built in functions that perform complex algorithms on the data
structures.
● Iterators are used to step through the elements of collection of object. These collections may
be containers or subset of container.
Sequence Containers: implement data structures which can be accessed in a sequential manner.
● vector
● list
● deque
● arrays
● forward_list
Associative Containers : implement sorted data structures that can be quickly searched (O(log n)
complexity).
◆ set
◆ multiset
◆ map
◆ multimap
#include<array>
● Creating object
array <object_type, array_size> array_name; //A <int> a1;
It create an empty array of object_type with maximum size of array_size.
Ex. array<int, 5> arr;
Operations on Array
1. at()
2. get()
3. operator[]
4. front()
5. back()
6. size()
7. max_size()
8. swap()
9. empty()
10. fill()
Iterators
Operations of Iterators:
1. begin()
2. end()
3. advance()
4. next()
5. prev()
6. inserter()
Iterator Functions
1. begin() : This function is used to return the beginning position of the container.
2. end() : This function is used to return the after end position of the container.
3. advance() : This function is used to increment the iterator position till the specified number
mentioned in its arguments.
4. next() : This function returns the new iterator that the iterator would point after advancing the
positions mentioned in its arguments.
5. prev() : This function returns the new iterator that the iterator would point after decrementing
the positions mentioned in its arguments.
6. inserter() : This function is used to insert the elements at any position in the container. It accepts
2 arguments, the container and iterator to position where the elements have to be inserted.
Vector
10 20 30 40 50 60 70
10 20 30 40 50 60 70 // new capacity
Iterator function in Vector
Iterators
2. end() : Returns an iterator pointing to the theoretical element that follows the last element in the
vector
3. rbegin() : Returns a reverse iterator pointing to the last element in the vector (reverse
beginning). It moves from last to first element
4. rend() : Returns a reverse iterator pointing to the theoretical element preceding the first element
in the vector (considered as reverse end)
Capacity function in Vector
Capacity
1. size() : Returns the number of elements in the vector.
2. max_size() : Returns the maximum number of elements that the vector can hold.
3. capacity() : Returns the size of the storage space currently allocated to the vector expressed as
number of elements.
4. resize(n) : Resizes the container so that it contains ‘n’ elements.
5. empty() : Returns whether the container is empty.
6. shrink_to_fit() : Reduces the capacity of the container to fit its size and destroys all elements
beyond the capacity.
Element Access function in Vector
Element access:
1. reference operator [g] : Returns a reference to the element at position ‘g’ in the vector
2. at(g) : Returns a reference to the element at position ‘g’ in the vector
3. front() : Returns a reference to the first element in the vector
4. back() : Returns a reference to the last element in the vector
5. data() : Returns a direct pointer to the memory array used internally by the vector to store its
owned elements.
Modifier functions in Vector
1. assign() : It assigns new value to the vector elements by replacing old ones
2. push_back() : It push the elements into a vector from the back
3. pop_back() : It is used to pop or remove elements from a vector from the back.
4. insert() : It inserts new elements before the element at the specified position
5. erase() : It is used to remove elements from a container from the specified position or range.
6. swap() : It is used to swap the contents of one vector with another vector of same type. Sizes may differ.
7. clear() : It is used to remove all the elements of the vector container
8. emplace() : It extends the container by inserting new element at position
9. emplace_back() : It is used to insert a new element into the vector container, the new element is added to
the end of the vector
List
● As compared to vector, the list has slow traversal, but once a position has been found, insertion and
deletion are quick.
● List class supports a bidirectional, linear list.
● List can be accessed sequentially only.
● List can be accessed from front to back or back to front.
● For creating a stack, we must include the <stack> header file in our code.
● We then use this syntax to define the std::stack:
Type – is the Type of element contained in the std::stack. It can be any valid C++ type or even a
user-defined type.
Container – is the Type of underlying container object.
Functions in Stack
3. top() : Returns a reference to the top most element of the stack – Time Complexity : O(1)
4. push(g) : Adds the element ‘g’ at the top of the stack – Time Complexity : O(1)
5. pop() : Deletes the top most element of the stack – Time Complexity : O(1)
Map
float f[4]; .5 .6 .2 .3
10 20 30 40 50 10 20 30 40 50