Standard Template Library is a library in C++ which deals with Data Structures
Size: 8.65 MB
Language: en
Added: Mar 18, 2021
Slides: 36 pages
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.
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
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.