The requirements (simplified) are ...
Using STL containers std::list, std::vector, and std::deque,
demonstrate the sorting of integers in descending order.
Code must be written in C++
Let us first discuss about each of these containers and their benefits before diving into examples for them :-
1. std::list - it is a container to store data in non contiguous manner, it is implemented using a doubly linked list and is faster compared to arrays in insertion and deletion operations
2. std::vector - It is a sequence container that enables dynamic size arrays,it can automatically expand / reduce on insertion or deletion respectively. It stores the data in contiguous memory spaces and can be accessed both via an iteration or a pointer to elements. Any new insertion happens in the end of the vector.
3.std::deque - They are called doubly ended queues and implemented on a data structure of the same name, they may or may not store data in contiguous memory and insertion and deletion are possible from both ends, it resembles a real life queue where insertion happens in the end and deletion happens in the front.
4. std::set is an associative container of unique objects of type Key that contains a sorted set .
Now coming to the given program :
#include <iostream>
#include <set>
#include <iterator>
#include <deque>
#include <list>
#include <bits/stdc++.h>
using namespace std;
int main()
{
/*
step 1 - create a set of unique integers using std::set
*/
set <int, greater <int> >
set1;
// insert data into the set
set1.insert(4);
set1.insert(5);
set1.insert(3);
set1.insert(6);
set1.insert(2);
set1.insert(5); // only one 5 will be added
set1.insert(1);
// check if data is entered correctly
set <int, greater <int> > :: iterator
itr;
cout << "\nThe initial set is : ";
for (itr = set1.begin(); itr != set1.end();
++itr)
{
cout << '\t' <<
*itr;
}
cout << endl;
/*
Declare the right representation of list , vector and deque to copy
all values from the set
using a for loop and interating through the set
*/
deque <int> deque_ex1; // include the header on top
list<int> list_ex1; // include header on top
vector<int> vec_ex1; // include header on top
// iterators to above mentioned data structures not needed as they
can add elements using push_back
// assigning the elements to the list, dequeue and the vector
for (itr = set1.begin(); itr != set1.end(); ++itr)
{
// loop thorugh all elements of
set
//add to vector
vec_ex1.push_back(*itr);
//add to deque
deque_ex1.push_back(*itr);
//add to list
list_ex1.push_back(*itr);
}
//shuffling elements
// using built-in random generator:
//1. for vector ;
std::random_shuffle (
vec_ex1.begin(), vec_ex1.end() );
//2. for deque
std::random_shuffle (
deque_ex1.begin(), deque_ex1.end() );
//3 for list
//since the shuffle function does
not work directly for lists it has to be converted to a vector and
then shuffled , that needs to be copied back to the list
vector<int> V(
list_ex1.begin(), list_ex1.end() ); // copy list to vector
std::random_shuffle(
V.begin(), V.end()); //shuffle the vector
list_ex1.assign( V.begin(),
V.end() ); // copy back to list
/*Displaying contents*/
// For vector
vector <int> :: iterator itr1;
cout << "The shuffled vector is:";
for (itr1 = vec_ex1.begin(); itr1 !=
vec_ex1.end(); ++itr1)
{
// loop thorugh all elements of
set
//add to vector
cout << '\t' <<
*itr1;
}
cout<<endl;
// For deque
deque <int> :: iterator itr2;
cout << "The shuffled deque is:";
for (itr2 = deque_ex1.begin(); itr2 !=
deque_ex1.end(); ++itr2)
{
// loop thorugh all elements of
set
//add to vector
cout << '\t' <<
*itr2;
}
cout<<endl;
// For list
list <int> :: iterator itr3;
cout << "The shuffled list is:";
for (itr3 = list_ex1.begin(); itr3 !=
list_ex1.end(); ++itr3)
{
// loop thorugh all elements of
set
//add to vector
cout << '\t' <<
*itr3;
}
cout<<endl;
cout<<endl;
//sorting the data structures
cout<<"Sorted data structures (using
system defined sort function):"<<endl;
sort (vec_ex1.begin(), vec_ex1.end());
cout << "The sorted vector is:";
for (itr1 = vec_ex1.begin(); itr1 !=
vec_ex1.end(); ++itr1)
{
// loop thorugh all elements of
set
//add to vector
cout << '\t' <<
*itr1;
}
cout<<endl;
// For deque
sort (deque_ex1.begin(), deque_ex1.end());
cout << "The sorted deque is:";
for (itr2 = deque_ex1.begin(); itr2 !=
deque_ex1.end(); ++itr2)
{
// loop thorugh all elements of
set
//add to vector
cout << '\t' <<
*itr2;
}
cout<<endl;
// For list
list_ex1.sort();
cout << "The sorted list is:";
for (itr3 = list_ex1.begin(); itr3 !=
list_ex1.end(); ++itr3)
{
// loop thorugh all elements of
set
//add to vector
cout << '\t' <<
*itr3;
}
cout<<endl;
return 0;
}
output
Get Answers For Free
Most questions answered within 1 hours.