Skip to main content

Implementation of STL Vector of User Defined Records in C++ | Functions of STL like Sort and Find

 Problem Statement: 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.

Note: Scroll Horizontally to see full line of code.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

class item
{
public:
    int itemCode, cost, quantity;
    string name;

    friend ostream &operator<<(ostream &output, item &D)
    {
        output << D.itemCode << "\t\t" << D.name << "\t" << D.cost << "\t" << D.quantity << endl;
        return output;
    }

    friend istream &operator>>(istream &input, item &D)
    {

        cout << "Enter the Item Code: ";
        input >> D.itemCode;
        cout << "Enter the name of item: ";
        input >> D.name;
        cout << "Enter the cost of item: ";
        input >> D.cost;
        cout << "Enter the quantity of item: ";
        input >> D.quantity;

        return input;
    }

    bool operator<(item &i1)
    {
        return (itemCode < i1.itemCode);
    }
    bool operator==(const item &i1)
    {

        if (itemCode == i1.itemCode)
            return 1;
        return 0;
    }
};

void display(vector<item> itemList)
{
    cout << "ItemCode"
         << "\t"
         << "Name"
         << "\tCost"
         << "\tQuantity" << endl;
    for (int i = 0; i < itemList.size(); i++)
    {
        cout << itemList[i];
    }
    cout << endl;
    cout << ".";
}

int main()
{
    vector<item> itemList;
    int n;
    cout << "Enter the no. of Items: ";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        item a;
        cin >> a;
        itemList.push_back(a);
        cout << endl;
    }
    display(itemList);
    char cont = 'y';
    while (cont == 'y')
    {
        int choice;
        cout << "\n1.Display Item List" << endl;
        cout << "2.Sort Item List" << endl;
        cout << "3.Search Item" << endl;
        cout << "\nEnter the Choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            display(itemList);
            break;
        case 2:
            sort(itemList.begin(), itemList.end());
            break;
        case 3:
            vector<item>::iterator p;
            item i1;
            cout << "\nEnter Item Code to search:";
            cin >> i1.itemCode;
            p = find(itemList.begin(), itemList.end(), i1);
            if (p == itemList.end())
            {
                cout << "\nNot found.";
            }
            else
            {
                cout << "\nFound.";
                cout << "ItemCode"
                         << "\t"
                         << "Name"
                         << "\tCost"
                         << "\tQuantity" << endl;
                    cout << *p;
            }
            break;
        }

        cout << "\nDo you want to continue? (y/n): ";
        cin >> cont;
    }
    if (cont == 'n')
    {
        cout << "Program Ended Successsfully" << endl;
    }

    return 0;
}

Comments