/* 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. */
#include <iostream> //standard input output stream header file
#include <algorithm> //The STL algorithms are generic because they can operate on a variety of data structures
#include <vector> //The header file for the STL vector library is vector.
using namespace std;
class Item
{
public:
char name[10];
int quantity;
int cost;
int code;
// operator overloading require here to search() // search code
bool operator==(const Item& i1) //Boolean operators allow you to create more complex conditional statements
{
if(code==i1.code) //operator will return 1 if the comparison is true, or 0 if he comparison is false
return 1;
return 0;
}
bool operator<(const Item& i1)
{
if(code<i1.code) //operator will return 1 if the comparison is true, or 0 if the comparison is false
return 1;
return 0;
}
};
vector<Item> o1; //declaring vector o1 for class item we create vector o1
void print(Item &i1);
void display();
void insert();
void search();
void dlt();
bool compare(const Item &i1, const Item &i2)
{
//if (i1.name != i2.name) return i1.cost < i2.cost;
return i1.cost < i2.cost; // sorting on cost
}
int main()
{
int ch;
do
{
cout<<"\n***** Menu *****";
cout<<"\n1.Insert";
cout<<"\n2.Display";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Delete";
cout<<"\n6.Exit";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort(o1.begin(),o1.end(),compare); //sort() function sorts the vector element in ascending order.
cout<<"\n\n Sorted on Cost";
display();
break;
case 5:
dlt();
break;
case 6:
exit(0);
}
}while(ch!=7);
return 0;
}
void insert()
{
Item i1;
cout<<"\nEnter Item Name:";
cin>>i1.name;
cout<<"\nEnter Item Quantity:";
cin>>i1.quantity;
cout<<"\nEnter Item Cost:";
cin>>i1.cost;
cout<<"\nEnter Item Code:";
cin>>i1.code;
o1.push_back(i1); // add elements to the end of vector by using push_back function//push_back fun supported by vector
}
void display()
{ //for each loop accepts a function which executes over each of the container elements. This loop is defined in the header file “algorithm”,
for_each(o1.begin(),o1.end(),print); // Execute print function from start to end of vector o1
}
void print(Item &i1)
{
cout<<"\n";
cout<<"\nItem Name:"<<i1.name;
cout<<"\nItem Quantity:"<<i1.quantity;
cout<<"\nItem Cost:"<<i1.cost;
cout<<"\nItem Code:"<<i1.code;
}
void search()
{
vector<Item>::iterator p; //Iterators are used to point at the memory addresses of STL containers
Item i1;
cout<<"\n Enter Item Code to search:";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1); //in-built function find
if(p==o1.end())
{
cout<<"\nNot found.";
}
else
{
cout<<"\nFound.";
}
}
void dlt()
{
vector<Item>::iterator p;
Item i1;
cout<<"\nEnter Item Code to delete:";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
{
cout<<"\nNot found.";
}
else
{
o1.erase(p);
cout<<"\nDeleted.";
}
}
Sunday, January 10, 2021
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment