C++ Programming Unit-5 SRM
C++ Programming Unit-5 SRM
AND PROGRAMMING
(CODE: 21CSC101T)
2nd Semester, B. Tech
2023-2024
UNIT-5
STL
CONTAINER
CONTAINER
ITERATOR DESCRIPTION
Read values with forward movement.
input_iterator
It can be incremented, compared, and dereferenced.
DERIVED CONTAINERS
(OR)
VECTOR LIST DEQUEUE SET MULTISET MAP MULTIMAP STACK QUEUE PRIORITY
QUEUE
SEQUENCE HEADER
DESCRIPTION ITERATOR
CONTAINERS FILE
A dynamic array.
ASSOCIATIVE HEADER
DESCRIPTION ITERATOR
CONTAINERS FILE
A set stores unique (no duplicate) set of
DERIVED HEADER
DESCRIPTION ITERATOR
CONTAINERS FILE
A standard stack.
Vectors are the dynamic arrays with the ability to resize them automatically when
an element is inserted or deleted.
CONSTRUCTORS PURPOSE
The default vector constructor takes no arguments, creates a
vector( )
new instance of that vector.
It is a default copy constructor that can be used to create a new
vector(const vector& C)
vector that is a copy of the given vector C.
The parameterized constructor creates a vector with space for
vector(size_type num, const TYPE& val = TYPE( )) num objects. If val is specified, each of those objects will be
given that value.
It creates a vector that is initialized to contain the elements
vector(iterator start, iterator end )
between start and end.
Mr. BAPUJI RAO, Dept. of CSE
VECTOR METHODS
14
METHODS PURPOSE
int vector_name.size( ); It returns the number of items in the vector.
void vector_name.push_back(element); It adds an element to the end of the vector.
void vector_name.assign( size_type num, const TYPE& val ); It assigns num copies of val to a vector.
void vector_name.assign( iterator start, iterator end ); It copies one vector to another from iterator
start to iterator end.
vector<data_type>::reverse_iterator iterator_name = It returns the reverse iterator to the end of the
vector_name.rbegin( ); vector.
vector<data_type>::reverse_iterator iterator_name = It returns the reverse iterator to the beginning
vector_name.rend( ); of a vector.
Mr. BAPUJI RAO, Dept. of CSE
EXAMPLE-1
15
#include<vector>
#include<iostream>
using namespace std;
#include<vector>
cout<<"\n Vector, v1 Elements:\n";
#include<iostream>
using namespace std; for(int i=0; i<v1.size(); i++)
int main() cout<<" "<<v1[i]<<endl;
#include<vector>
#include<iostream> // copying a vector in another vector
using namespace std; v3.assign(in1,in2);
int main()
METHODS PURPOSE
It returns a reference to last element of a
vector<Data_Type> ::iterator vector_name.back( );
vector.
It returns a reference to the first element
METHODS PURPOSE
vector<Data_Type> ::iterator vector_name.insert( iterator loc, const TYPE& It inserts val before loc, returning an
val ); iterator to the element inserted.
cout<<"\n\n The Vector, alpha After Inserting 4 Z at the Begin :\n\n ";
cout<<"\n\n Appending v1 at the end of v2. \n Now, The Vector, v2 :\n\n ";
for( int i=0; i<v2.size(); i++ )
{
METHODS PURPOSE
int list_name.size( ); It returns the number of items in the list.
void list_name.push_back(element); It adds an element to the end of the list.
METHODS PURPOSE
list<Data_Type> ::iterator List_Name.back( ); It returns a reference to last element of a list.
list<Data_Type>::iterator List_Name.front( ); It returns a reference to the first element of a list.
// Demonstration of size().
#include <list>
#include<iostream>
cout<<endl;
return 0;
}
METHODS PURPOSE
int deque_name.size( ); It returns the number of items in the deque.
void deque_name.push_back(element); It adds an element to the end of the deque.
METHODS PURPOSE
deque<Data_Type> ::iterator deque_name.back( ); It returns a reference to last element of a deque.
deque<Data_Type>::iterator deque_name.front( ); It returns a reference to the first element of a deque.
// Demonstration of assign() to store 6 copies of the integer 40 into a deque and display it.
#include <deque>
#include<iostream>
#include<iterator>
CONSTRUCTORS PURPOSE
The default set constructor takes no arguments, creates a new instance of that
set( )
METHODS PURPOSE
begin It returns an iterator to the beginning of the set.
// Demonstration of set constructors and size() method. cout<<"\n Size of set1 = "<<set1.size();
#include <iostream> cout<<"\n\n Size of set2 = "<<set2.size();
#include <vector> cout<<"\n\n Size of set3 = "<<set3.size();
#include <set>
Syntax-1:
set<Data_Type, less<Data_Type> > Set_Name(Array_Name, Array_Name + Array_Size);
cout <<"\n The Names From The Set After Deletion of 'Balu' And 'Robin':";
for(iter = nameSet.begin();iter != nameSet.end(); iter++)
cout <<"\n "<<*iter;
cout<<endl;
return 0;
}
#include <iostream>
cout<<endl;
return 0;
}
Mr. BAPUJI RAO, Dept. of CSE
MAP EXAMPLE-2
53
cout << "\n Bhanu is " << ages["Bhanu"] << " years old" << endl;
cout << "\n\n The First Map in Original Order: " << endl<<endl;
cout<<"\n States\t\tCapitals\n";
cout<<" ------\t\t-------\n";
for(iter = mapStates.begin(); iter != mapStates.end(); iter++)
cout <<" "<<iter->first <<"\t\t" <<iter->second <<endl ;
//cout <<" "<<(*iter).first <<"\t\t" <<(*iter).second <<endl ;
cout<<endl;
return 0;
}
Mr. BAPUJI RAO, Dept. of CSE
MULTIMAP EXAMPLE-1
57
It is a container adapter that gives the programmer the functionality of a stack - a FILO (first-
in, last-out) or LIFO(last-in, first-out) data structure.
METHODS PURPOSE
// Demostration of stack program cout<<"\n The Stack Elements After Popping Out Three Elements:\n";
#include<iostream> while (!s.empty())
#include<stack> {
using namespace std; cout <<" "<< s.top() <<"\n";
if(ptr==arr+8)
cout<<endl<<" "<<num<<" Not Found\n\n";
else
cout<<"\n First "<<num<<" found at Index=" <<(ptr-arr)<<endl<<endl;
return 0;
} Mr. BAPUJI RAO, Dept. of CSE
count( ) ALGORITHM
65
#include<iostream>
#include <algorithm> cout<<"\n\n The Ascending Sorted Array arr1:\n";
#include<functional> // for greater<>( ) for(int i=0; i<8; i++)
using namespace std; cout<<' '<<arr1[i];
Syntax:
merge(Container1_Begin_Address, Container1_End_Address,
Container2_Begin_Address, Container2_End_Address, Resultant_Container)
Syntax:
for_each(Container_Begin_Address, Container_End_Address, Function_For_Result);
It changes the container items and stores in the same container or different container.
Syntax:
tranform(Container_Begin_Address, Container_End_Address, New_Container,
REVERSE INSERT
ITERATOR ITERATOR ostream_iterator istream_iterator
RAW STORAGE
ITERATOR
list<int>::iterator iter;
cout<<"\n The List:\n";
for(iter=mylist.begin(); iter!=mylist.end(); iter++)
cout<<' '<<*iter; //displaying output
The ostream_iterator object can be used as an argument to any algorithm that specifies an output iterator.
Syntax:
ostream_iterator<Data_Type> Output_Iterator_Name(cout, "Separator Character");
The istream_iterator object can be used as an argument to any algorithm that specifies an input iterator.
Syntax:
istream_iterator<Data_Type> Input_Iterator_Name(cin);
THANK YOU