standard template library

33
STANDARD TEMPLATE LIBRARY BY: SUKRITI SINGH A0523113081 BTBM/13/242 AMITY INSTITUTE OF BIOTECHNOLOGY AMITY UNIVERSITY, NOIDA Teacher Incharge: Mr. Asit Dwivedi

Upload: sukriti-singh

Post on 12-Apr-2017

588 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Standard template library

STANDARD TEMPLATE LIBRARY

BY:SUKRITI SINGHA0523113081BTBM/13/242AMITY INSTITUTE OF BIOTECHNOLOGYAMITY UNIVERSITY, NOIDA

Teacher Incharge:Mr. Asit Dwivedi

Page 2: Standard template library

Library Classes

To help programmers be more productive, C++ includes predefined classes in the form of packages as part of the installation. These are called ‘library classes.’

It offers many packages through its libraries. Packages, in turn, provide thousands of classes that provides tens of thousands of methods for carrying out diverse type of tasks.

Page 3: Standard template library

STANDARD TEMPLATE LIBRARY (STL)

T he C++ ST L (Standard Template Library) is a powerful set of C++ template classes to provide general-purpose templatized classes and functions that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks.

Standard Template Library (STL) stands for standard template library and is basically a library of many useful containers or algorithms.

In layman terms , it basically is a class template with functions already created making life easier as they need to be merely called to be used.

The STL achieves its results through the use of templates. This approach is very powerful, delivering compile-time polymorphism that is often more efficient than traditional run-time polymorphism. Modern C++ compilers are tuned to minimize any abstraction penalty arising from heavy use of the STL.

Page 4: Standard template library

The Standard Template Library was created as the first library of generic algorithms and data structures, with four ideas in mind: generic programming, abstractness without loss of efficiency, the Von Neumann computation model, and value semantics.

The package file in C++ is stored in “namespace std” (namespace groups different identifiers in a named scope.), hence before using the library classes we need to call this directive as using namespace std;

Algorithm Containers

Function Objects

Iterators

Function Objects

Page 5: Standard template library

Components of STL

Containers

Algorithms

Iterators

Page 6: Standard template library

Container is a way that stored data is organized in memory, for example an array of elements.

Algorithms in the STL are procedures that are applied to containers to process their data, for example search for an element in an array, or sort an array.

Iterators are a generalization of the concept of pointers, they point to elements in a container, for example you can increment an iterator to point to the next element in an array.

Page 7: Standard template library

• Containers vector, list, map, hash_map, …

iterators

• Algorithm

Sort, find, search, copy, …

Algorithms manipulate data, but don’t know

about containers

Containers store data, but don’t

know about algorithms

Algorithms and containers interact through iterators

Each container has its own iterator types

Page 8: Standard template library

ContainersA container

is a way to store data, either built-

in data types like

int and float, or

class objects.

Generic "off-the-

shelf“ class templates for storing collections

of data.

Containers are used to

manage collections

of objects of a certain

kind.

There are several

different types of

containers like deque, list, vector, map etc.

Page 9: Standard template library

Kinds of Containers

Sequence

• Store elements in linear sequence

• Vectors, Linked Lists, and derivatives

Adaptors

• Used to automatically arrange stored data in a specific order. Uses trees

• Queue, Priority_Queue, Stack

Associative

• Special purpose containers created from normal containers

• Hash, Map, Set, Multimap, Multiset

Page 10: Standard template library

Sequence Containers

Special purpose containers created from normal containers.These containers store and retrieve data in a sequential fashion.

A sequence container stores a set of elements in sequence, in other words each element (except for the first and last one) is preceded by one specific element and followed by another.

In an ordinary C++ array the size is fixed and can not change during run-time, it is also tedious to insert or delete elements. Advantage: quick random access

Page 11: Standard template library
Page 12: Standard template library

Stores elements by position

Each item in the list has both a value and a memory address (pointer) that identifies the next item in the sequence

To access a specific data value in the list, one must start at the first position (front) and follow the pointers from element to element until data item is located.

Basic Steps to Work in Sequence Containers

Page 13: Standard template library

vector– dynamic array

• Offers random but ‘direct’ access.• Back insertion• Should be your default choice, but choose wisely• Compatible with C : &v[0] points to the first element• Treated as an array with the capability of growing or shrinking

dynamically. The elements can be accessed in two ways:i. using the overloaded operator ( )ii. using the method at

• The first one is easier and faster to use, but it is not range checked.

• On the other hand, the method does range checking and throws out_of_range exception if needed.

Star

t

0 1 2 3

(hold sequences in difference ways)

Page 14: Standard template library

deque– double-ended queue (usually array of arrays)

o Offers random accesso Back and Front insertiono Slower than vectorso No C - compatibilityo This is basically a double-ended queue.o If we want to grow/shrink in a deque, we can do it at both the ends.o It provides the same accessing efficiency as the vector but the allocation

efficiency comparable with a list.

0 1 2 3Star

t

Page 15: Standard template library

list– 'traditional' doubly linked list

Don't expect random access You can insert anywhere though, Arrays are optimised for random

access but are inefficient when it comes to inserting and deleting elements in the middle; so are the vector and deque.

For operations requiring intensive insertions and deletions, use a list, which is very efficient for these operations (and is internally implemented as a double-linked list).

With a list, you cannot have random access to the data and so the operator is not overloaded.

2 3Star

t

0 1

Page 16: Standard template library

Associative Containers

• As the name suggest ‘Associate’ is linking it to different nodes.

• Used to automatically arrange stored data in a specific order. Uses trees.

• Tress provide and facilitates faster searching.• An associative container is non-sequential but

uses a key to access elements.• The keys, typically a number or a string, are

used by the container to arrange the stored elements in a specific order

• for example in a dictionary the entries are ordered alphabetically.

Page 17: Standard template library

Trees : They are non-linear data structures, where a unique path exists to traverse(moving ahead or accessing) a particular node. Binary trees can utmost have 2 nodes, read either as Pre,In or Post.

Node: It is a self referential data structure, which works on sending reference of its location to link of sets or maps.

Link list: It is a serial collection of nodes. While this is a linear data structure.

Array is a serial memory allocation while lists having serial nodes have a reference to the memory hence allocated randomly and linked to each other through references.

Non-linear data structure engages

memory in non sequential order.

Linear data structures engages

memory linearly or in a sequential order.

Hence, we can conclude

that

Comparison of Trees, Lists, Arrays

Page 18: Standard template library
Page 19: Standard template library

Maps:• Stores unique key/value pairs• key is associated with only one value (One-

to-One Mapping).• Rapid key-based lookup.

Sets:• Store the elements along with a unique key.• The values are irrelevant and we keep track

of the keys only• Rapid lookupCommon Points:• These two are interchangeable.• No duplicates• They provide an important functionality,

Maps and Sets

Page 20: Standard template library

Begin

820

3

1

5

7

4 6

Page 21: Standard template library

Multisets and Multimaps:

These are extended versions of a map and set espectively.Multisets: • Stores non-unique sets• duplicates are allowed, Rapid lookup.

Multimaps:• Stores non-unique key/value pairs• one key may be associated with more than one value (One-to-Many

Mapping)• Duplicates are allowed• Rapid key based lookup

Page 22: Standard template library

Adapter Containers

Also known as Derived Containers.

Provide different ways to access sequential &

associative containers.

They are implemented using the containers

seen before

They do not provide actual data structure

Container adapters do not support iterators

The functions push and pop are common

to all container adapters

component that adapts (modifies) an existing interface of a component to expose

a different one suitable for some

purpose.

These containers restrict how elements

enter and leave a sequence

Page 23: Standard template library
Page 24: Standard template library

Stack LIFOLast-in-first-

out data structure

They are implemented with vector, list, and deque (by default)

Allows access at only one end of the sequence (top)

Adds objects to container by pushing the object onto the stack

Removes objects from container by popping the stack

Page 25: Standard template library

Queue FIFOFirst in First Out Data Structure.

Implemented with list and deque (by default).

Allows access only at the front and rear of the sequence.

Items enter at the rear and exit from the front.

Example: waiting line at a grocery store.

Page 26: Standard template library

Priority Queue • Insertions are done in a sorted order

• Deletions from front similar to a queue

• They are implemented with vector (by default) or deque

• The elements with the highest priority are removed first

• Operations are similar to those of a stack or queue

• Elements can enter the priority queue in any order

• Once in the container, a delete operation removes the largest (or smallest) value

• Example: a filtering system that takes in elements and then releases them in priority order

18 13 3 15

Page 27: Standard template library

Algorithm

In mathematics and computer science, an algorithm is a step-by-

step procedure.

It is a specific set of instructions for carrying

out a procedure or solving a problem

Usually with the requirement that the

procedure terminate at some point.

Specific algorithms sometimes also go by

the name method, procedu

re, or technique.

Page 28: Standard template library

Algorithms typically take iterators as arguments

Container provides a way to access its elements using iterators.

Algorithms act on containers.

Provide the means by which you will perform initialization, sorting , searching , and transforming of the contents of containers.

Page 29: Standard template library

Iterators Iterators are generalised pointers and act as the

glue between containers and algorithms.

STL algorithms are written in terms of iterator parameters, and STL containers provide iterators that can be plugged into algorithms.

Generally, iterators point to a location within a container.

Iterators have a pointer-like syntax (in many cases, iterators are indeed implemented as pointers internally).

Page 30: Standard template library

Iterators are used to step throug h the

elements of collections of objects.These collections

may be containers or subsets of containers.Iterators are pointer-like

entities that are used to access individual elements in

a container.Often they are used to move sequentially from element to

element, a process called iterating through a container.One can have multiple

iterators pointing to different or identical elements in the

container

Page 31: Standard template library

Container Type of iterator supported

Sequence containers

vector random access deque random access list bidirectional

Associative containers set bidirectional multiset bidirectional map bidirectional multimap bidirectional

Container adapters stack no iterators supported queue no iterators supported priority_queue

no iterators supported

Page 32: Standard template library

Compiling STL Container

AlgorithmIterator

Container

Iterator

Algorithm

Objects

Iterator

Iterator

Algorithm

Page 33: Standard template library

Thank You!!!!