C++_Programming_Unit-5[1]
C++_Programming_Unit-5[1]
STL
CONTAINER
ITERATOR
ALGORITHM ITERATOR CONTAINER
ITERATOR
CONTAINER
CONTAINER
5
A Container is a collection of OBJECTS.
The objects actually stores data.
These containers are implemented by TEMPLATE CLASSES.
ITERATOR
7
ITERATOR DESCRIPTION
Read values with forward movement.
input_iterator
It can be incremented, compared, and dereferenced.
CLASSIFICATION OF CONTAINERS
9
DERIVED CONTAINERS
(OR)
VECTOR LIST DEQUEUE SET MULTISET MAP MULTIMAP STACK QUEUE PRIORITY
QUEUE
SEQUENCE HEADER
DESCRIPTION ITERATOR
CONTAINERS FILE
A dynamic array.
ASSOCIATIVE CONTAINERS
11
ASSOCIATIVE HEADER
DESCRIPTION ITERATOR
CONTAINERS FILE
A set stores unique (no duplicate) set of
DERIVED HEADER
DESCRIPTION ITERATOR
CONTAINERS FILE
A standard stack.
VECTOR
13
CONSTRUCTORS PURPOSE
The default vector constructor takes no arguments, creates
vector( )
a new instance of that vector.
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.
EXAMPLE-1
15
#include<vector>
#include<iostream>
using namespace std;
int main()
EXAMPLE-2
17
#include<vector> OUTPUT
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;
LIST
19
CONSTRUCTORS PURPOSE
The default list constructor takes no arguments, creates a new
list( )
instance of that list.
It is a default copy constructor that can be used to create a new
list(const list& C)
list that is a copy of the given list C.
The parameterized constructor creates a list with space for num
list(size_type num, const TYPE& val = TYPE( )) objects. If val is specified, each of those objects will be given
that value.
It creates a list that is initialized to contain the elements between
list(iterator start, iterator end )
start and end.
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.
EXAMPLE-1
21
// Demonstration of size().
#include <list>
#include<iostream>
cout<<endl;
return 0;
}
EXAMPLE-3
23
// Demonstration of merging of two lists
#include <list>
#include<iostream>
#include<iterator>
using namespace std;
Continue…
25
cout<<"\n\n The List4 Elements:";
for(it=list4.begin(); it!=list4.end(); it++)
{
cout <<"\n "<<*it;
}
DEQUE
27
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.
EXAMPLE-1
29
EXAMPLE-3
31
In an associative container the items are not arranged in sequence, but arranged in a Tree
Structure.
The two main categories of associative containers are SETS and MAPS.
SET CONSTRUCTORS
33
CONSTRUCTORS PURPOSE
The default set constructor takes no arguments, creates a new
set( )
METHODS PURPOSE
It returns an iterator to the beginning of the set.
EXAMPLE-1
35
MULTISET EXAMPLE
37
// Example of multiset constructors // declaration of set iterator
and set<int>::iterator it;
// size() method. cout<<"\n\n Items of set1 = ";
#include <iostream> for(it=set1.begin(); it!=set1.end(); it++)
#include <vector> cout<<" "<<*it;
#include <set>
Syntax:
map<string, string/int/float less<string> > Map_Name;
CONTINUE…
39
cout<<endl;
return 0;
}
Dr. BAPUJI RAO, Department of CSE(CORE)
MULTIMAP EXAMPLE
40
CONTINUE…
41
EXAMPLE
43
// Example 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";
EXAMPLE
// To find the first object with a specified value 45
#include <iostream>
#include <algorithm> //for find( ) algorithm
using namespace std; 1st RUN
int arr[ ] = { 10, 40, 30, 40, 50, 30, 60, 70 };
int main()
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;
} Dr. BAPUJI RAO, Department of CSE(CORE)
count( ) ALGORITHM
46
sort( ) ALGORITHM
47
#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];
search( ) ALGORITHM
49
#include <iostream>
#include <algorithm>
using namespace std;
int array[ ] = { 11, 44, 33, 11, 22, 33, 11, 22, 44 };
merge( ) ALGORITHM
51
#include <iostream>
#include <algorithm> //for merge()
using namespace std;
int arr1[ ] = { 2, 3, 4, 6, 8 };
for_each( ) ALGORITHM
53
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.
EXAMPLE
55
OUTPUT
REVERSE INSERT
ITERATOR ITERATOR ostream_iterator istream_iterator
RAW STORAGE
ITERATOR
Dr. BAPUJI RAO, Department of CSE(CORE)
CONTINUE…
59
ostream_iterator CLASS
61
The ostream_iterator object can be used as an argument to any algorithm that specifies an
output iterator.
Examples:
ostream_iterator<int> cout_iterator1(cout, " ");
ostream_iterator<int> cout_iterator2(cout, " , ");
copy(mylist.begin(), mylist.end(), cout_iterator1);
copy(mylist.begin(), mylist.end(), cout_iterator2);
Dr. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
62
istream_iterator CLASS
63
The istream_iterator object can be used as an argument to any algorithm that specifies an
input iterator.
Examples:
list<float> mylist(5);
istream_iterator<float> input_iterator(cin);
istream_iterator<float> end_of_stream;
copy(input_iterator, end_of_stream, mylist.begin( ));
Dr. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
64
// Example of istream_iterator
#include <iostream>
#include <list>
CONTINUE…
65
//display mylist
ostream_iterator<float> ositer(cout, "\n ");
cout<<"\n\n The Numbers from the List:\n ";
copy(mylist.begin(), mylist.end(), ositer);
cout << endl;
return 0;
}