STANDARD TEMPLATE LIBRARY
Developed By Mrs. K.M.Sanghavi STANDARD TEMPLATE LIBRARY (STL)
Developed by Alexander Stepanov and Meng Lee of HP in 1979.
Standard template library accepted in July 1994 into C++ ANSI Standard
These are called as collection of General-purpose template classes( data structures) and functions 1 COMPONENTS OF STL
Containers
Algorithms
Iterators
2 COMPONENTS OF STL
Algorithms use iterators to interact with objects stored in containers Container
Algorithm1 Algorithm 2
Object1 Object2 Iterator 1 Iterator 2
Object3 Iterator 3
Algorithm 3 3 CONTAINER
Objects that hold Example : Array data (of same type)
Implemented by Template Classes
4 ALGORITHM
Example : These are procedures used to process the data Searching, Sorting, contained in containers. Merging, Copying, Initializing
Implemented by template functions
5 ITERATOR
It is an object that Used to move points to an through the element in a contents of container container
They can be Connect Algorithms incremented and with Containers decremented 6 COMPONENTS OF STL
Containers
7 CATEGORIES OF CONTAINERS
Sequence Containers
Derived Associative Containers Containers
8 CONTAINERS
STL Defines 10 Containers
9 CATEGORIES OF CONTAINERS
Sequence Associative Derived
• vector • set • stack • deque • multiset • queue • list • map • Priority_queue • multimap
10 SEQUENCE CONTAINERS
Stores elements in a linear sequence
Each element is related to other elements by its position along the line
They allow insertion of elements
Example
Element Element Element Last …….. 0 1 2 element 11 THREE TYPES OF SEQUENCE CONTAINERS
vector
deque list
12 Vector : Sequence Container
Expandable and dynamic array
Grows and shrinks in size
Insertion / Deletion of elements at back
Permits direct access to any element
13 Vector : Sequence Container
Container Header File Iterator vector
14 vector Sequence Container
Declarations ◦ vector
15 vector Sequence Container
vector functions ◦ v.push_back(value) Add element to end (found in all sequence containers). ◦ v.size() Current size of vector ◦ v.capacity() How much vector can hold before reallocating memory Reallocation doubles size ◦ vector
16 vector Sequence Container
vector functions ◦ v.insert(iterator, value ) Inserts value before location of iterator ◦ v.insert(iterator, array , array + SIZE) Inserts array elements (up to, but not including array + SIZE) into vector ◦ v.erase( iterator ) Remove element from container ◦ v.erase( iter1, iter2 ) Remove elements starting from iter1 and up to (not including) iter2 ◦ v.clear() Erases entire container 17 vector Sequence Container
vector functions operations ◦ v.front(), v.back() Return first and last element ◦ v.[elementNumber] = value; Assign value to an element
18 Vector : Sequence Container int array[5] = {12, 7, 9, 21, 13 }; vector
12 7 9 21 13 v.pop_back(); v.push_back(15);
12 7 9 21 12 7 9 21 15
0 1 2 3 4
12 7 9 21 15
19 v.begin(); v[3] Vector : Sequence Container #include 13 21 9 7 12 12 7 9 21 13 21 Vector : Using Iterator #include vec1.push_back(10); vec1.push_back(7); vec1.push_back(3); 22 Vector : Using Iterator cout<<"vec1 data: "; for(int i=0; i cout<<"\nOperation: vec1.begin()\n"; vec1_Iter = vec1.begin(); cout<<"The first element of vec1 is "<<*vec1_Iter< cout<<"\nOperation: vec1.rbegin()\n"; vec1_rIter = vec1.rbegin(); cout<<"The first element of the reversed vec1 is “; cout<<*vec1_rIter< Operation: vec1.begin() The first element of vec1 is10 Operation: vec1.rbegin() The first element of the reversed vec1 is : 3 24