Compare the container classes of Lists Queuesand Stacks and

Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues

and describe what each one does and how, and write a C++ example of the each type that would be suited for that structure.

Submit the cpp files.

Thank you

Solution

Solution:

The container is a holder object which is used to store the collection of different objects and it is implemented as class templates.

List:

List is a sequence container, It performs a insert and delete operation in a constant time with in sequence and it can iterate both directions.

It is implemented in double linked list and stores the element in different and unrelated locations.

Example code:

#include <iostream>

#include <list>

using namespace std;

int main(void)

{

     // create list container

     list<char> lstelm;

     char chr;

    

     cout<<\"elm list is: \";

    

     // insert elements from \'a\' to \'z\'

     for( chr = \'a\'; chr <= \'z\'; ++ chr)

     lstelm.push_back( chr);

     // print and remove the element from list

     while(!lstelm.empty())

     {

          cout<<lstelm.front()<<\' \';

          lstelm.pop_front();

     }

     cout<<endl;

     return 0;

}

Queues:

Queue is a container specially designed to perform the operation First in First Out, the elements are inserted into one end and removed from another end.

Example code:

// STL queue

#include <queue>

#include <vector>

#include <list>

#include <iostream>

using namespace std;

int main(void)

{

     //Declare the queue and push the value

     queue <int, vector<int> > qu3;

     qu3.push(110);

         

     // copies elements from a container

     list<int> li;

     li.push_back(13);

     li.push_back(17);

     li.push_back(12);

     li.push_back(19);

     queue <int, list<int> > qu2(li);

     cout<<\"The front of queue qu2 is \"<<qu2.front()<<endl;

     cout<<\"The back of queue qu2 is \"<<qu2.back()<<endl;

     return 0;

}

Stacks:

Stack is a container specially designed to perform the operation Last in First Out, the element insertion and deletion is performed in one end.

Example code:

// C++ STL stack

#include <stack>

#include <iostream>

using namespace std;

int main(void)

{

     stack <int> stk1;

     int jval;

    

     // push data into stack

     cout<<\"Push operation in stack...\"<<endl;

     stk1.push(2);

     jval = stk1.top();

     cout<<jval<<\' \';

     stk1.push(19);

     // get top of the stack

     jval = stk1.top();

     cout<<jval<<\' \';

     stk1.push(2);

     jval = stk1.top();

     cout<<jval<<\' \';

     stk1.push(13);

     jval = stk1.top();

     cout<<jval<<\' \'<<endl;

     stack <int>::size_type istk;

     istk = stk1.size();

    

     // print some data

     cout<<\"The stack length is \"<<istk<<endl;

     istk = stk1.top();

     cout<<\"The top element of the stack is \"<<istk<<endl;

     stk1.pop();

     istk = stk1.size();

     cout<<\"The stack length after pop is \"<<istk<<endl;

     istk = stk1.top();

     cout<<\"The element at the top of the stack is \"<<istk<<endl;

     return 0;

}

Sets:

Set is a container, it stores a unique elements in a specific order.

Example code:

#include <set>

#include <iostream>

using namespace std;

char main(void)

{

     // iterators

     set <char>::iterator st_Iter;

    

     // empty set st0

     set <char> st0;

    

     // empty set st1

     set <char, less<char> > st11;

    

     // insert or push data

     st11.insert(\'a\');

     st11.insert(\'b\');

     st11.insert(\'e\');

     st11.insert(\'F\');

     st11.insert(\'h\');

     st11.insert(\'J\');

    

     // create a set by copying the range st11

     set <char>::const_iterator st1_PIter, st1_QIter;

     st1_PIter = st11.begin();

     st1_QIter = st11.begin();

     st1_QIter++;

     st1_QIter++;

    

     set <char> st2(st1_PIter, st1_QIter);

     cout<<\"st5 set data: \";

     for(st_Iter = st2.begin(); st_Iter != st2.end(); st_Iter++)

     cout<<\" \"<<*st_Iter;

     cout<<endl;

    

     return 0;

}

Maps:

Map is an associative container; the elements stored in the map as the combination of key value and mapped value in a specific order.

Example code:

#include <map>

#include <iostream>

using namespace std;

int main(void)

{

     // simple map pair<int, int> to pairint

     typedef pair<int, int> pairint;

    

     // iterators

     map<int, int>::iterator mp0_Iter, mp1_Iter;

     map<int, int, greater<int> >::iterator mp2_Iter;

    

     // create an empty map map1

     map <int, int, less<int> > map1;

     map1.insert(pairint(11, 23));

     map1.insert(pairint(13, 33));

     map1.insert(pairint(13, 41));

     map1.insert(pairint(12, 33));

     map1.insert(pairint(16, 35));

     map1.insert(pairint(19, 35));

         

     //map with the allocator of map

     map <int, int>::allocator_type mp1_Alloc;

     mp1_Alloc = map1.get_allocator();

     map <int, int> mp3(less<int>(), mp1_Alloc);

     mp3.insert(pairint(1, 10));

     mp3.insert(pairint(2, 12));

    

     // map mp5 by copying the range map1

     map <int, int>::const_iterator mp1_PIter, mp1_QIter;

     mp1_PIter = map1.begin();

     mp1_QIter = map1.begin();

     mp1_QIter++;

     mp1_QIter++;

     map <int, int> mp5(mp1_PIter, mp1_QIter);

     cout<<\"map1 map data: \";

for(mp1_Iter = map1.begin(); mp1_Iter != map1.end(); mp1_Iter++)

     cout<<\" \"<<mp1_Iter->second;

     cout<<endl;

    

     return 0;

}

Priority Queues:

Priority Queue is a type of a container adaptor, it is specially designed to have first element always greatest in the elements it contains.

Example code:

#include <queue>

#include <vector>

#include <deque>

#include <list>

#include <iostream>

using namespace std;

int main(void)

{

     // Declares priority_queue

     priority_queue <int, deque <int> > mypq1;

     cout<<\"mypq1 = \";

     mypq1.push(212);

     mypq1.push(250);

     mypq1.push(120);

     // print the values

     while (!mypq1.empty())

     {

          cout<<mypq1.top()<<\" \";

          mypq1.pop();

     }

     cout<<endl;

    

     system(\"pause\");

     return 0;

}

Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o
Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o
Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o
Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o
Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o
Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o
Compare the container classes of Lists, Queues,and Stacks and Sets, Maps, and Priority Queues and describe what each one does and how, and write a C++ example o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site