Understanding STL in C++: Containers, Iterators, Algorithms, and Smart Pointers
STL (Standard Template Library) in C++ encompasses containers like list, vector, set, algorithms utilizing iterators, and smart pointers to handle memory leak issues. Learn how these components work together to manage data efficiently using templates and generic classes.
Uploaded on Dec 12, 2024 | 0 Views
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
UNIT VI Introduction 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
STL (Standard template library ) Collection of generic classes and functions is called standard template library ( STL ) Components of STL Containers Algorithm Iterators These three work in conjunction with one another Algorithm employs iterators to perform operations stored in containers 2
Algorithm 1 Algorithm 2 Container Iterator 2 Iterator 1 Object 1 Object 2 Object 3 Iterator 3 Algorithm 3 3 Relation ship between three STL Components
Description Container 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 containers Are implemented using template functions Algorithms 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
Containers It holds data Containers Sequence Containers Derived Containers Associative Containers Vector Deque list Stack Queue Set Multiset Map mutimap Priority_queue 5
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
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
Derived containers The 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 push 8
containers Container Description Header file Iterator Sequence Containers Vector Dynamic array, Direct access to any element, insertion and deletion at back <vector> Random access List Bidirectional, linear list, insertion and deletion anywhere <list> bidirectional Deque Double ended queue, insertion and deletion both the ends, direct access to any element <deque> Random access 9
Container Description Header file Iterator Associative containers Set Storing unique sets <set> Bidirectional Multiset Stores non-unique sets <set> Bidirectional Map Stores key/ value pair, each key associated with one value, allows key based look up <map> Bidirectional Multi map Stores key/ value pair, One key associated with more than one value (one- many mapping) <map> Bidirectional 10
Container Description Header file Iterator Derived containers Stack LIFO <stack> No Iterator Queue FIFO <queue> No Iterator Priority queue First element out with always highest priority element <queue> No Iterator 11
Algorithm Algorithms are the functions used for processing contents of their containers 60 algorithms available To use these include <algorithm> Categories Retrieve or non-mutating algorithms Mutating algorithms Sorting algorithms Set algorithm Relational algorithms All functions available 406 Balagurusamy (3rd edition) 12
Iterators Like pointers Used to access container elements Traverse from one element to another, process known as iterating through the container iterators 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 items Iterators available Input linear, forward Output linear, forward Forward - linear, forward Bidirectional linear, forward and backward Random random, forward and backward 13
Container classes Vector class #include<vector.h> Important functions of the vector class At() Back() Iterator Begin() Capacity() Clear() Empty() End() Erase() Insert() Pop_back)() Push_back() Resize() Size() Swap() 14
Vectors class demo program Void 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); // add one more elements v.push_back(10.5); //call size() and display() // using iterators vector <int> :: iterator itr = v.begin(); itr = itr + 3; // itr points to 4th element v.insert(itr,9); // call display() //delete 4th element v.erase(v.begin()+3); // call display() } 15
Functions of list container Back() Begin() Clear() Empty() End() Erase() Insert() Merge() Pop_back() Pop_front() Push_back() Push_front() Remove() Resize() Reverse() Size() Sort() Swap() 16
List class #include <list> Void display(list<int> &lis ){ list<int> :: iterator p; for(p=lis.begin(); p!=lis.end();p++) cout<<*p; // add two elements at the ends list1.push_front(100); list1.push_back(200); List2.pop_front(); Display(list1); Display(list2); Void main(){ list<int> list1; list<int> list2(5); List<int>listA, listB; listA=list1; listB=list2; 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); //merging two lists List1.merge(list2); Display(list1); listA.sort(); listB.sort(); listA.reverse(); 17
Map class Functions under Map Class Begin() Clear() Empty() End() Erase() Find() Insert() Size() Swap() 18