Unit - 5 Oodp Final
Unit - 5 Oodp Final
Unit - 5 Oodp Final
Components of STL
• Iterators are pointer like entities that are used to access the
elements stored in the container.
• You will have to start from the beginning and traverse through
the list until the desired element is found.
Random Access
Input Forward
Output Bidirectional
{
int myints[]={10,20,30,40};
int *p;
p=std::find(myints, myints+4, 30);
if (p!= myints+4)
std::cout<<“Element found in myints:”<<*p<<“\n”;
else
std::cout<<“Elements not found in myints \n”;
return 0;
}
Default
• Template <class RandomAccessIterator>
• Void sort (RandomAccessIterator first, RandomAccessIterator
last);
Custom
• Template <class RandomAccessIterator, Class Compare>
• Void sort (RandomAccessIterator first, RandomAccessIterator
last, Compare comp);
• The element are compared using operator < for the first
version and comp for the second.
Custom
• Template<class InputIterator1, class InputIterator2, class
OutputIterator, class Compare>
• OutputIterator merge (InputIterator1 first1, InputIterator1
last1, InputIterator2 first2, InputIterator2 last2, OutputIterator
result, Compare comp);
File Operations
• Opening and Closing a file
• A file can be opened in two ways
• Using the constructor of the class
• Using the member function open() of the class
streampos begin,end;
ifstream myfile(“example.bin”, ios::binary);
begin=myfile.tellg(); //current pos of the file
myfile.seekg(0, ios::end); // moves ptr to the end of file
end=myfile.tellg();
myfile.close();
cout<<“Size is:”<<(end-begin)<<“bytes”;
return 0;
Output
} Size is: 40 bytes
goodbit 0 value