/
UNIT VI Introduction to STL, Components of STL – Containers, Iterators and Algorithms, UNIT VI Introduction to STL, Components of STL – Containers, Iterators and Algorithms,

UNIT VI Introduction to STL, Components of STL – Containers, Iterators and Algorithms, - PowerPoint Presentation

tracy
tracy . @tracy
Follow
69 views
Uploaded On 2023-06-21

UNIT VI Introduction to STL, Components of STL – Containers, Iterators and Algorithms, - PPT Presentation

minmax algorithm header files Smart pointers concept shared pointers concept memory leak problem STL Standard template library Collection of generic classes and functions is called standard template library STL ID: 1001143

containers list display int list containers int display vector list2 push data container list1 random elements queue access iterator

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "UNIT VI Introduction to STL, Components ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. UNIT VIIntroduction to STL, Components of STL – Containers, Iterators and Algorithms, List, Vector, set, minmax, algorithm header files. Smart pointers concept, shared pointers concept, memory leak problem

2. STL (Standard template library )Collection of generic classes and functions is called standard template library ( STL )Components of STLContainersAlgorithmIteratorsThese three work in conjunction with one anotherAlgorithm employs iterators to perform operations stored in containers2

3. 3Algorithm 1Algorithm 2Algorithm 3Object 1Object 2Object 3Iterator 1Iterator 2Iterator 3Container Relation ship between three STL Components

4. DescriptionContainer is an object that actually stores the data. It is the way the data is organized in memory. 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.Algorithm is a procedure that is used to process the data contained in containersAre implemented using template functionsAlgorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of containers.Iterator is an object( like pointer ) that points to an element in a container.Use iterators to move through the contents of containers Iterators are used to step through the elements of collections of objects. These collections may be containers or subsets of containers 4

5. ContainersIt holds data5ContainersSequence ContainersVectorDequelistAssociative ContainersSetMultisetMapmutimapDerived ContainersStackQueuePriority_queue

6. sequence containers refer to a group of container class templates in the standard library of the C++ that implement storage of data elements. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. One common property of all sequential containers is that the elements can be accessed sequentially. The following containers are defined in the current revision of the C++ standard: array, vector, list, deque. Each of these containers implements different algorithms for data storage, which means that they have different speed guarantees for different operations:array implements a compile-time non-resizable array.vector implements an array with fast random access and an ability to automatically resize when appending elements.deque implements a double-ended queue with comparatively fast random access.list implements a doubly linked list.forward_list implements a singly linked list.6

7. associative containers refer to a group of class templates in the standard library of the C++ that implement ordered associative arrays. Being templates, they can be used to store arbitrary elements, such as integers or custom classes.All these containers store data in a structure called tree which facilitates fast searching, deletion and insertion. These are slow for random access and inefficient for sorting.7

8. Derived containersThe STL provides three derived containers namely stack, queue and priority queue. These are also known as container adaptors.The derived containers do not support iterators. We cannot use them for data manipulation.They support two member function pop and push8

9. containers9ContainerDescriptionHeader fileIteratorSequence ContainersVectorDynamic array, Direct access to any element, insertion and deletion at back<vector>Random accessListBidirectional, linear list, insertion and deletion anywhere<list>bidirectionalDequeDouble ended queue, insertion and deletion both the ends, direct access to any element <deque>Random access

10. 10ContainerDescriptionHeader fileIteratorAssociative containersSetStoring unique sets<set>BidirectionalMultisetStores non-unique sets<set>BidirectionalMapStores key/ value pair, each key associated with one value, allows key based look up<map>BidirectionalMulti mapStores key/ value pair,One key associated with more than one value (one- many mapping)<map>Bidirectional

11. 11ContainerDescriptionHeader fileIteratorDerived containersStackLIFO<stack>No IteratorQueueFIFO<queue>No IteratorPriority queueFirst element out with always highest priority element <queue>No Iterator

12. AlgorithmAlgorithms are the functions used for processing contents of their containers60 algorithms availableTo use these include <algorithm>CategoriesRetrieve or non-mutating algorithmsMutating algorithmsSorting algorithmsSet algorithmRelational algorithms12All functions available 406 Balagurusamy (3rd edition)

13. IteratorsLike pointersUsed to access container elementsTraverse from one element to another, process known as iterating through the containeriterators provide a means for accessing data stored in container classes such a vector, map, list, etc. iterator as pointing to an item that is part of a larger container of itemsIterators availableInput – linear, forwardOutput – linear, forwardForward - linear, forwardBidirectional – linear, forward and backward Random – random, forward and backward13

14. Container classesVector class #include<vector.h>Important functions of the vector classAt()Back()Iterator Begin()Capacity()Clear()Empty()End()Erase()Insert()Pop_back)()Push_back()Resize()Size()Swap()14

15. Vectors class demo programVoid display(vector<int> &v){for(i =0 to v.size()) cout<<v[i]}int main(){ vector<int> v; int x; for(i= 0 to <5){ cin>>x; v.push_back(x); } v.size(); Display(v); 15// add one more elementsv.push_back(10.5);//call size() and display()// using iteratorsvector <int> :: iterator itr = v.begin();itr = itr + 3; // itr points to 4th elementv.insert(itr,9);// call display()//delete 4th elementv.erase(v.begin()+3);// call display()}

16. Functions of list containerBack()Begin()Clear()Empty()End()Erase()Insert()Merge()Pop_back()Pop_front()Push_back()Push_front()Remove()Resize()Reverse()Size()Sort()Swap()16

17. List class#include <list>Void display(list<int> &lis ){list<int> :: iterator p;for(p=lis.begin(); p!=lis.end();p++) cout<<*p;Void main(){ list<int> list1; list<int> list2(5);For(i=0 to <5) list1.push_back(10); list<int> :: iterator p;For(p list2.begin() to !list2.end() ) *p=20;Display(list1);Display(list2); 17// add two elements at the endslist1.push_front(100);list1.push_back(200);List2.pop_front();Display(list1);Display(list2);List<int>listA, listB;listA=list1;listB=list2;//merging two listsList1.merge(list2);Display(list1);listA.sort();listB.sort();listA.reverse();

18. Map classFunctions under Map ClassBegin()Clear()Empty()End()Erase()Find()Insert()Size()Swap()18