Standard Template Library (STL) : Programs

*******************PROGRAM 1***********************
/*
 * C++ Program to Implement Stack in Stl
 */

#include <iostream>
#include <stack>
#include <cstdlib>
using namespace std;
int main()
{
    stack<int> st;
    int choice, item;
    while (1)
    {
        cout<<"\n---------------------"<<endl;
        cout<<"Stack Implementation in Stl"<<endl;
        cout<<"\n---------------------"<<endl;
        cout<<"1.Insert Element into the Stack"<<endl;
        cout<<"2.Delete Element from the Stack"<<endl;
       cout<<"3.Size of the Stack"<<endl;
        cout<<"4.Top Element of the Stack"<<endl;
        cout<<"5.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter value to be inserted: ";
            cin>>item;
            st.push(item);
            break;
        case 2:
            item = st.top();
            st.pop();
        cout<<"Element "<<item<<" Deleted"<<endl;
            break;
        case 3:
        cout<<"Size of the Queue: ";
        cout<<st.size()<<endl;
            break;
        case 4:
        cout<<"Top Element of the Stack: ";
        cout<<st.top()<<endl;
            break;
        case 5:
            exit(1);
        break;
        default:
            cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

*******************PROGRAM 2***********************
// PROGRAM TO DEMONSTRATE USE OF VECTOR STL.

#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
   // create a vector to store int
   vector<int> vec; 
   int i;

   // display the original size of vec
   cout << "vector size = " << vec.size() << endl;

   // push 5 values into the vector
   for(i = 0; i < 5; i++){
      vec.push_back(i);
   }

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;

   // access 5 values from the vector
   for(i = 0; i < 5; i++){
      cout << "value of vec [" << i << "] = " << vec[i] << endl;
   }

   // use iterator to access the values
   vector<int>::iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "value of v = " << *v << endl;
      v++;
   }

   return 0;
}

*******************PROGRAM 3***********************
//PROGRAM TO DEMONSTRATE MAP STL

#include <iostream>
#include <map>
using namespace std;
typedef map<string,int> phonebook ;

//defined map as string-int pair

int main()
{

string name;
int number;
phonebook phone;
cout<<"Enter name and contact number of your three friends";
for(int i=0;i<3;i++)
{
        cin>>name;
        cin>>number;
        phone[name]=number;
}
cout<<"\nAdding one more friend as <Comp-1111111> as pair";
phone["Comp"]=1111111;
cout<<"\nAdding one more friend as <Engg-9999999999> as pair";
phone.insert(pair<string,int> ("Engg",99999));
int n=phone.size();
cout<<"\nSize of Map is: "<<n;
cout<<"\nList of phone numbers is :\nName\tNumber\n";
phonebook::iterator itr;
for(itr=phone.begin();itr!=phone.end();itr++)
{
        cout<<(*itr).first<<"\t"<<(*itr).second<<endl;
}
cout<<"Enter name to search:\n";
cin>>name;
number=phone[name];
cout<<"Number of "<<name<<" is "<<number;

return 0;
}

*******************PROGRAM 4***********************

// Demonstration of deque in STL
#include <deque>
#include <iostream>
using namespace std;
int main(void)
{
// deque container
deque <int> dq1,dq2;
// deque iterator
deque <int>::iterator Iter;
// push/insert data into deque
dq1.push_back(10);
dq1.push_back(20);
dq1.push_front(30);
dq2.push_back(40);
dq2.push_back(50);
dq2.push_front(60);
// print the data and do some operations
cout<<"dq1 deque: ";
for (Iter = dq1.begin(); Iter != dq1.end(); Iter++ )
cout<<*Iter<<" ";
cout<<endl;
Iter++;
//insert 1-- at iter position
dq1.insert(Iter, 100);
cout<<"\nOperation: dq1.insert(Iter, 100);"<<endl;
cout<<"dq1 deque: ";
for (Iter = dq1.begin(); Iter != dq1.end(); Iter++)
cout<<*Iter<<" ";
cout<<endl;
Iter = dq1.begin();
Iter++;
Iter++;
//insert 200 twice at iter position
dq1.insert(Iter, 2, 200);
cout<<"\nOperation: dq1.insert(Iter, 2, 200);"<<endl;
cout<<"dq1 deque: ";
for (Iter = dq1.begin(); Iter != dq1.end(); Iter++)
cout<<*Iter<<" ";
cout<<endl;
//insert at begin++ in dq1 values from dq2 begin to dq2 end
dq1.insert(++dq1.begin(), dq2.begin(),dq2.end());
cout<<"\nOperation: dq1.insert(++dq1.begin(), dq2.begin(),dq2.end());"<<endl;
cout<<"dq1 deque: ";
for (Iter = dq1.begin(); Iter != dq1.end(); Iter++)
cout<<*Iter<<" ";
cout<<endl;
//delete elements from deque
cout<<"\nElements deleted from deque are:\n";
for (Iter = dq1.begin(); Iter != dq1.end(); Iter++)
{
dq1.pop_front();
cout<<*Iter<<" ";
}
cout<<endl;
return 0;
}


*******************PROGRAM 5***********************

// C++ STL list example

#include <iostream>
#include <list>
using namespace std;
int main(void)
{
// list container for character elements
list<char> elem;
char chs;
cout<<"elem list is: ";
// append elements from 'a' to 'z'
for(chs = 'a'; chs <= 'z'; ++chs)
elem.push_back(chs);
// while there are elements, print and remove the element
while(!elem.empty())
{
cout<<elem.front()<<' ';
elem.pop_front();
}
cout<<endl;
return 0;

}
 
*************************PROGRAM 6 ******************
// C++ STL queue, pop()

#include <queue>
#include <iostream>
using namespace std;
int main(void)
{
queue <int> que;

// push/insert data

que.push(21);

que.push(9);

que.push(13);

 

int i = que.size();

// print the data

cout<<"The queue length is "<<i<<endl;

i = que.front();

cout<<"The element at the front of the queue is "<<i<<endl;

// do the popping

que.pop();

i = que.size();

cout<<"After a pop the queue length is "<<i<<endl;

i = que.front();

cout<<"After a pop, the element at the front of the queue is "<<i<<endl;

return 0;

}

*********************PROGRAM 7 ********************
//DEMONSTRATION OF SET STL

#include<set>
#include<iostream>
using namespace std;
int main()
{
    int a[5]={2,4,44,22,89};
    set<int> S(a,a+5);
set<int>::iterator itr;
for(itr=s.begin();itr!=s.end();itr++)
{
    cout<< *itr<<"\t";
}
cout<<"Size is "<<s.size();
sort(a,a+5);
//to sort set
sort(s.begin(),s.end());
cout<<"Enter element to search ";
int elm;
cin>>elm;
if(itr=binary_search(s.begin(),s.end(),elm))
cout<<"element found";
else
{
    cout<<"element doesnot exit";
    exit(0);
}
cout<<"which element you want to insert";
cin>>elm;
s.insert(itr,elm);
int min=s.min_element(s.begin(),s.end());
int max=s.max_element(s.begin(),s.end());
//Sorting using heap sort
//create a heap using make_heap function
//and use heap_sort for sorting

make_heap(a,a+5);
heap_sort(a,a+5);

make_heap(s.begin(),s.end());
heap_sort(s.begin(),s.end());

return 0;
}

************************PROGRAM 8 *******************
 // C++ STL list constructors

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// list iterators

list <int>::iterator li0Iter, li1Iter, li2Iter, li3Iter, li4Iter, li5Iter, li6Iter;

// create an empty list li0

list <int> li0;

// create a list li1 with 10 elements of default value 0

list <int> li1(10);

// create a list li2 with 8 elements of value 7

list <int> li2(8, 7);

// create a list li3 with 9 elements of value 8 and with the allocator of list li2

list <int> li3(9, 8, li2.get_allocator());

// li4, a copy of list li2

list <int> li4(li2);

// create a list li5 by copying the range of li4[_First, _Last)

li4Iter = li4.begin();

li4Iter++;

li4Iter++;

li4Iter++;

li4Iter++;

list <int> li5(li4.begin(), li4Iter);

// create a list li6 by copying the range of li4[_First, _Last) and with the allocator of list li2

li4Iter = li4.begin();

li4Iter++;

li4Iter++;

li4Iter++;

list <int> li6(li4.begin(), li4Iter, li2.get_allocator());

 

// do some operations

cout<<"Operation: list <int> li0;"<<endl;

cout<<"li0 list data: ";

for(li0Iter = li0.begin(); li0Iter != li0.end(); li0Iter++)

cout<<" "<<*li0Iter;

cout<<endl;

cout<<"\nOperation: list <int> li1(10);"<<endl;

cout<<"li1 list data: ";

for(li1Iter = li1.begin(); li1Iter != li1.end(); li1Iter++)

cout<<" "<<*li1Iter;

cout<<endl;

cout<<"\nOperation: list <int> li2(8, 7);"<<endl;

cout<<"li2 list data: ";

for(li2Iter = li2.begin(); li2Iter != li2.end(); li2Iter++)

cout<<" "<<*li2Iter;

cout<<endl;

cout<<"\nOperation: list <int> li3(9, 8, li2.get_allocator());"<<endl;

cout<<"li3 list data: ";

for(li3Iter = li3.begin(); li3Iter != li3.end(); li3Iter++)

cout<<" "<<*li3Iter;

cout<<endl;

cout<<"\nOperation: list <int> li4(li2);"<<endl;

cout<<"li4 list data: ";

for(li4Iter = li4.begin(); li4Iter != li4.end(); li4Iter++)

cout<<" "<<*li4Iter;

cout<<endl;

cout<<"\nOperation1: li4Iter = li4.begin(), li4Iter++..."<<endl;

cout<<"Operation2: list <int> li5(li4.begin(), li4Iter);"<<endl;

cout<<"li5 list data: ";

for(li5Iter = li5.begin(); li5Iter != li5.end(); li5Iter++)

cout<<" "<<*li5Iter;

cout<<endl;

cout<<"\nOperation1: li4Iter = li4.begin(), li4Iter++..."<<endl;

cout<<"Operation2: list <int> li6(li4.begin(), li4Iter, li2.get_allocator());"<<endl;

cout<<"li6 list data: ";

for(li6Iter = li6.begin(); li6Iter != li6.end(); li6Iter++)

cout<<" "<<*li6Iter;

cout<<endl;

return 0;

}

************************PROGRAM 9 *******************
//PROGRAM TO DEMONSTRATE USE OF NESTED CONTAINER

 #include <stack>

#include <vector>

#include <iostream>

using namespace std;

int main(void)

{

// declare stacks with vector base containers

stack <int, vector<int> > s1, s2, s3;

int j;

 

// the following would have cause an error because stacks with different

// base containers are not equality comparable

//

// stack <int, list<int> > s3;

//

// push/insert data

s1.push(1);

s1.push(2);

s1.push(3);

s2.push(2);

s3.push(1);

s3.push(2);

s3.push(3);

 

// print the data

cout<<"s1 stack data: ";

for(j = s1.top(); j != int(s1.empty()); j--)

cout<<j<<' ';

cout<<endl;

cout<<"s2 stack data: ";

for(j = s2.top(); j != int(s2.empty()); j--)

cout<<j<<' ';

cout<<endl;

cout<<"s3 stack data: ";

for(j = s3.top(); j != int(s3.empty()); j--)

cout<<j<<' ';

cout<<endl;

cout<<"\nOperation: s1 != s2?"<<endl;

if (s1 != s2)

cout<<"The stacks s1 and s2 are not equal."<<endl;

else

cout<<"The stacks s1 and s2 are equal."<<endl;

cout<<"Operation: s1 != s3?"<<endl;

if (s1 != s3)

cout<<"The stacks s1 and s3 are not equal."<<endl;

else

cout<<"The stacks s1 and s3 are equal."<<endl;

return 0;

}