1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Data Structures operations using STL(Standard Template Library)

Discussion in 'C++' started by seeguna, Jul 27, 2007.

  1. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    Data Structures operations using STL(Standard Template Library)
    Code:
    #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
    	
    }
     
  2. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    You had the same Article more than once. I deleted the other one.
     
  4. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    i will correct that in my next article.
    Thanx for ur suggestion
     

Share This Page