Standard Template Library

GauravPatil318 650 views 36 slides Mar 18, 2021
Slide 1
Slide 1 of 36
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36

About This Presentation

Standard Template Library is a library in C++ which deals with Data Structures


Slide Content

Standard Template Library SY – CS – C – Group 5

What is a Standard Library ? In the C++ programming language, the Standard Library is a collection of classes and functions. The C++ Standard Library can be categorized into two parts − 1. The Standard Function Library  2. The Object-Oriented Class Library

What are Templates ? The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. C++ adds two new keywords to support templates:  ‘template’  and  ‘ typename ’ . The second keyword can always be replaced by keyword ‘class’.  

How Templates work Templates are expanded at compiler time. The difference is, compiler does type checking before template expansion. The source code contains only function/class, but compiled code may contain multiple copies of same function/class.

What is Standard Template Library ? 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. A working knowledge of template classes is a prerequisite for working with STL.

Components of STL

STL Component Overview Data Storage Data Access Algorithms

Containers Containers or container classes store objects and data. 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.

3 Types of Container

Sequence Container

What is Vector Class? Vector is a template class in STL (Standard Template Library) of C++ programming language. These are sequence containers that store elements. They can automatically manage storage. It is efficient if you add and delete data often.

Some Methods of Vector Class

Example of Vector Class Vector <int> v = { 12 , 7 , 9 , 21 , 13 } ; 12 7 9 21 13 12 7 9 21 13 v.pop_back () ; 12 7 9 21 13 ….. 12 7 9 21 15 v.push_back (15) ; 12 7 9 21 13 0 1 2 3 4 v.begin () ; v[3] v.end () ;

What is a List? Sequence Containers - allow constant time insert and erase operations. Store elements they contain in different and unrelated storage locations. Lists - better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained, and therefore also in algorithms that make intensive use of these, like sorting algorithms. Consume some extra memory to keep the linking information associated to each element.

Some Operations of List Class Operations Uses sort Sort elements in a list splice Transfer elements from 1 list to another list merge Merge sorted lists unique Removes duplicate elements swap Exchange the content of 1 list with that of another list. assign Assigns new contents to the list remove Erase all instances of value

Example of List Class list <int> li = { 12 , 7 , 9 , 21 , 13 } ; 12 7 9 21 13 12 7 9 21 13 li.pop_back () ; 12 7 9 21 13 ….. 12 7 9 21 15 v.push_back (15) ; 12 7 9 21 7 9 21 15 12 li.pop_front () ; 14 7 9 21 15 ….. v.push_front (14) ;

Deque Class Sequence containers with the feature of expansion and contraction on both the ends. Implemented as dynamic arrays. To use deque container in a program include statement: #include <deque> Syntax: Deque declaration deque <elementType> name(size);

Some Operations of Deque Class Operation Description insert Inserts an element. maxsize Return the max no of elements. pushfront Push elements into a deque from the front. pushback Push elements into a deque from the back. popfront Pop/remove elements from a deque from the front. popback Pop/remove elements from a deque from the back. clear Remove all the elements of the deque container.

Container Adaptors

Types of Container Adaptors Queue:   A  queue  can store elements in the sequence container  list  or  deque  data structure. Priority queue: The  priority queue  adapter class is a little bit different in the sense that it enables the insertion of elements in sorted order into the data structure and removal of elements occurs from the front of the waiting line. Stack: The class called  stack  is an implementation of the stack data structure where insertion and retrieval of elements occurs according to the FIFO manner

Associative Containers

Types of Iterators

Iterators What are iterators? How many types of iterators are provided by C++ ? Can iterators be null in C++ ?

Iterators 17 17 23 12 17 4 array size 4 Iterators are pointer like entries that are used to access individual elements in the container Often, they are used to move sequentially from element to element , a process called iterating through container.

Iterators 17 17 23 12 17 4 array size 4 The Member function begin() and end() return an iterator to the first and past the last element of a container 17 v.begin () 12 v.end ()

Algorithms C ollection of functions especially designed to be used on ranges of elements. They act on containers and provide means for various operations for the contents of the containers. R ange - Sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the  STL containers .

Some of the most used Algorithms

Sort Algorithm

Reverse Algorithm

Max element Algorithm

Min element Algorithm

Accumulate Algorithm

Find Algorithm

Count Algorithm

References David Vandevoorde  and  Nicolai M. Josuttis  (2002). C++ Templates: The Complete Guide. Addison-Wesley Professional.  ISBN   0-201-73484-2 . Nicolai M. Josuttis  (2000).  The C++ Standard Library: A Tutorial and Reference . Addison-Wesley.  ISBN   0-201-37926-0 . Atul Saini  and  David R. Musser . STL Tutorial and Reference Guide: C+ + Programming with the Standard Template Library.

Thank You