Question

The requirements (simplified) are ... Using STL containers std::list, std::vector, and std::deque, demonstrate the sorting of...

The requirements (simplified) are ...

Using STL containers std::list, std::vector, and std::deque,

demonstrate the sorting of integers in descending order.

  1. Use std::set to create a data set of unique integers.
  2. Copy the data set to the other containers.
  3. Shuffle the containers.
  4. Display each container's contents.
  5. Use appropriate sorting algorithms to sort the data in each container.
  6. Redisplay each container's contents.

Code must be written in C++

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Lists are members of a general category of abstract data types (ADTs) called containers (i.e., objects...
Lists are members of a general category of abstract data types (ADTs) called containers (i.e., objects whose purpose is to hold other objects). A list is a collection of items having the following defining characteristics: Homogeneity: All the items are of the same type. Linearity: Each item has a unique predecessor (except the first) and a unique successor (except the last). Variable Length: The number of items can vary over time. Order: Items may be ordered (i.e., as in a...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Suppose that you want to add items to an array such that the items are always...
Suppose that you want to add items to an array such that the items are always ordered in ascending order; e.g., [1, 2, 2, 4, 5, 8, 9, 14, 32], and any duplicate values are adjacent to each other. We haven’t talked about sorting algorithms yet, so assume you want to be able to keep the items in the array in order without having to sort them. So for example, suppose you want to add the integer value 7 to...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT