Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Data Structures operations using STL(Standard Template Library) (http://www.go4expert.com/articles/data-structures-operations-using-stl-t5486/)

seeguna 27Jul2007 11:44

Data Structures operations using STL(Standard Template Library)
 
Data Structures operations using STL(Standard Template Library)
Code: Cpp

#include<iostream>
#include<vector>
#include<algorithm>// Included for Algorithmic  functions such as sort,max_element,binary_search  etc
#include<stdlib.h>
using namespace std;

void main()
{
    int i,number,array[20],choice,srch;
    cout<<"\n Enter a number of elements:";
    cin>>number;
    cout<<"Enter the number one by one\n";
    for(i=0;i<number;i++){
        cin>>array[i];
    }
    vector <int>::iterator loop_iterate;//loop iterator
    vector <int>::iterator max_iterate;// To store maximum element
    vector <int>::iterator min_iterate;// To store minimum element
    vector <int> vec_first(array,array+number);//vector created for our array
    while(1)
    {
       
        cout<<"\n1.Sorting\n2.Searching\n3.Max/Min Element\n4.Exit\n";
        cout<<"Enter ur choice:\n";
        cin>>choice;
        switch(choice)
        {
        case 1:
           
            sort(vec_first.begin(),vec_first.end());
            cout<<"\nAfter Sorting\n";
            for(loop_iterate=vec_first.begin();loop_iterate<vec_first.end();loop_iterate++){
                cout<<*loop_iterate<<endl;
            }
            break;
           
        case 2:
            cout<<"Enter the element to be searched:\n";
            cin>>srch;
           
            //Binary search works well for sorted array
            //so u have to sort the array first and then apply binary search algorithm
            sort(vec_first.begin(),vec_first.end());
            if(binary_search(vec_first.begin(),vec_first.end(),srch))
               
                cout<<"Element found";
           
            else
                cout<<"Element not found";
            break;
           
        case 3:
           
            max_iterate=max_element(vec_first.begin(),vec_first.end());
            cout <<"\n Maximum element present in the array is :"<<*max_iterate<<endl;
           
            min_iterate=min_element(vec_first.begin(),vec_first.end());
            cout <<"\n Minimum element present in the array is :"<<*min_iterate<<endl;
           
            break;
        case 4:
            exit(0);
           
        }
    }  //end of while loop
   
}


Shishir191 27Jul2007 17:06

Re: Data Structures operations using STL(Standard Template Library)
 
Hi,
Your topic is good. But i think presentation is not looking good to me. Its not easy to understand the code because you have written everthing in the main and its very difficult to read. Its my assumption.

shabbir 27Jul2007 18:25

Re: Data Structures operations using STL(Standard Template Library)
 
You had the same Article more than once. I deleted the other one.

seeguna 31Jul2007 18:44

Re: Data Structures operations using STL(Standard Template Library)
 
i will correct that in my next article.
Thanx for ur suggestion


All times are GMT +5.5. The time now is 20:50.