OOP Group C1
OOP Group C1
OOP Group C1
4 4 4 4 4 20
----------------------------------------------------------------------------------------------------------------
Assignment No: 6
----------------------------------------------------------------------------------------------------------------
Title of the Assignment:
Personnel information system using sorting and searching for STL and vector
container.
----------------------------------------------------------------------------------------------------------------
Problem Statement:
Write C++ program using STL for sorting and searching user defined records such as personal
records (Name, DOB, Telephone number etc) using vector container.
OR
Write C++ program using STL for sorting and searching user defined records such as Item records
(Item code, name, cost, quantity etc) using vector container.
----------------------------------------------------------------------------------------------------------------
Prerequisite:
1. Object Oriented Programming
---------------------------------------------------------------------------------------------------------------
Objective of the Assignment: To learn the concept of file handling.
---------------------------------------------------------------------------------------------------------------
Outcomes:
1. Learning of STL.
Department of Computer Engineering Subject : OOPCGL
---------------------------------------------------------------------------------------------------------------
Software & Hardware Requirements:
CPP Compiler
---------------------------------------------------------------------------------------------------------------
Theory:
STL:
The Standard Template Library (STL) is a set of C++ template classes to provide common
programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container
classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized.
A working knowledge of template classes is a prerequisite for working with STL.
STL has four components
• Algorithms
• Containers
• Functions
• Iterators
Algorithms
• The algorithm defines a collection of functions especially designed to be used on ranges of
elements.They act on containers and provide means for various operations for the contents of
the containers.
• Algorithm
• Sorting
• Searching
• Important STL Algorithms
• Useful Array algorithms
• Partition Operations
• Numeric
Containers
• Containers or container classes store objects and data. There are in total seven standard “firstclass”
container classes and three container adaptor classes and only seven header files that
provide access to these containers or container adaptors.
• Sequence Containers: implement data structures which can be accessed in a sequential manner.
• vector
• list
• deque
• arrays
Department of Computer Engineering Subject : OOPCGL
sort(startaddress, endaddress)
startaddress: the address of the first element of the array
endaddress: the address of the next contiguous location of the last element of the array.
So actually sort() sorts in the range of [startaddress,endaddress)
//Sorting
#include <iostream>
#include <algorithm>
usingnamespacestd;
voidshow(inta[])
{
for(inti = 0; i < 10; ++i)
cout<< a[i] << " ";
}
intmain()
{
inta[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
cout<< "\n The array before sorting is : ";
show(a);
sort(a, a+10);
cout<< "\n\n The array after sorting is : ";
show(a);
return0;
}
The output of the above program is :
The array before sorting is : 1 5 8 9 6 7 3 4 2 0
The array after sorting is : 0 1 2 3 4 5 6 7 8 9
Searching:
It is a widely used searching algorithm that requires the array to be sorted before search is applied.
The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the
element is found, or all the elements are exhausted.
It works by comparing the middle item of the array with our target, if it matches, it returns true
Department of Computer Engineering Subject : OOPCGL
otherwise if the middle term is greater than the target, the search is performed in the left sub-array.
If the middle term is less than target, the search is performed in the right sub-array.
The prototype for binary search is :
binary_search(startaddress, endaddress, valuetofind)
startaddress: the address of the first element of the array.
endaddress: the address of the last element of the array.
valuetofind: the target value which we have to search for.
//Searching
#include <algorithm>
#include <iostream>
usingnamespacestd;
voidshow(inta[], intarraysize)
{
for(inti = 0; i <arraysize; ++i)
cout<< a[i] << " ";
}
intmain()
{
inta[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
intasize = sizeof(a) / sizeof(a[0]);
cout<< "\n The array is : ";
show(a, asize);
cout<< "\n\nLet's say we want to search for 2 in the array";
cout<< "\n So, we first sort the array";
sort(a, a + asize);
cout<< "\n\n The array after sorting is : ";
show(a, asize);
cout<< "\n\nNow, we do the binary search";
if(binary_search(a, a + 10, 2))
cout<< "\nElement found in the array";
else
cout<< "\nElement not found in the array";
cout<< "\n\nNow, say we want to search for 10";
Department of Computer Engineering Subject : OOPCGL