Question

In: Computer Science

C++ How to make this code take a class object instead of int for the vector...

C++
How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object?

#include <iostream>
#include <float.h>
#include <bits/stdc++.h>
using namespace std;
 
void print_queue(queue<int> Q) //This function is used to print queue
{
   while (!Q.empty())
   {
       cout<< Q.front() << " ";
       Q.pop();
   }
}
 
int main()
{
   int n = 10;
   vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue of type int
 
   array_queues[0].push(3); //Push elements into queue at 0th index of vector
   array_queues[0].push(4);
   array_queues[0].push(5);
 
   array_queues[3].push(11); //Push elements into queue at 3rd index of vector
   array_queues[3].push(1);
   array_queues[3].push(8);
 
   array_queues[6].push(31); //Push elements into queue at 6th index of vector
   array_queues[6].push(55);
   array_queues[6].push(101);
   array_queues[6].push(-221);
 
   cout<<"\n\n";
   for(int i = 0; i < n; i++) //For each element of vector, print its queue
   {
       cout << "index = " << i << ", Queue: ";
       print_queue(array_queues[i]);
       cout << endl;
   }
   array_queues[6].pop(); //Dequeuing elements from queue at 6th index of vector
 
 
   cout<<"\n\nAfter Dequeuing from 6th index\n";
   for(int i = 0; i < n; i++) //For each element of vector, print its queue
   {
       cout << "index = " << i << ", Queue: ";
       print_queue(array_queues[i]);
       cout << endl;
   }
   return 0;
}
 

Solutions

Expert Solution

//Modified C++ code

#include <iostream>
#include <float.h>
#include <bits/stdc++.h>
using namespace std;

class Integer{
   private:
       int data;
      
   public:
       Integer(int data){
           this->data = data;
       }
       int getData(){
           return data;
       }
};

void print_queue(queue<Integer> Q){
   while (!Q.empty()){
       cout<< Q.front().getData() << " ";
       Q.pop();
   }
}

int main()

{

int n = 10;

vector< queue<Integer> > array_queues(n); //Create vector of queues of size 10, each entry has a queue of type int
Integer num1(3);
Integer num2(4);
Integer num3(5);
Integer num4(11);
Integer num5(1);
Integer num6(8);
Integer num7(31);
Integer num8(55);
Integer num9(101);
Integer num10(-221);

array_queues[0].push(num1); //Push elements into queue at 0th index of vector

array_queues[0].push(num2);

array_queues[0].push(num3);

array_queues[3].push(num4); //Push elements into queue at 3rd index of vector

array_queues[3].push(num5);

array_queues[3].push(num6);

array_queues[6].push(num7); //Push elements into queue at 6th index of vector

array_queues[6].push(num8);

array_queues[6].push(num9);

array_queues[6].push(num10);

cout<<"\n\n";

for(int i = 0; i < n; i++) //For each element of vector, print its queue

{

cout << "index = " << i << ", Queue: ";

print_queue(array_queues[i]);

cout << endl;

}

array_queues[6].pop(); //Dequeuing elements from queue at 6th index of vector

cout<<"\n\nAfter Dequeuing from 6th index\n";

for(int i = 0; i < n; i++) //For each element of vector, print its queue

{

cout << "index = " << i << ", Queue: ";

print_queue(array_queues[i]);

cout << endl;

}

return 0;

}


Related Solutions

Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
Complete the Vec class to make it similar to the vector. sample code here. #include #include...
Complete the Vec class to make it similar to the vector. sample code here. #include #include #include #include #include using namespace std; int get_mode( vector vec ) { int numbers[101] = {0}; for ( int e : vec ) numbers[e]++; int greatest = 0, n = 0; for ( int i = 0; i <= 100; i++ ) { if ( numbers[i] > greatest ) { greatest = numbers[i]; n = i; } } return n; } const double g...
You will work with MyInteger class (like an int, but it will be an object) by...
You will work with MyInteger class (like an int, but it will be an object) by obtaining the two files, MyInteger.h and testMyInteger.cpp. Create the file MyInteger.cpp by implementing the member and friend functions for the class. ==============MyInteger.h============== #ifndef MYINTEGER_H #define MYINTEGER_H #include <iostream> using namespace std; class MyInteger { public: MyInteger(int v = 0); void setValue(int v); int getValue() const; MyInteger operator+(const MyInteger &r) const; MyInteger operator-(const MyInteger &r) const; bool operator==(const MyInteger &r) const; friend ostream &operator<<(ostream &out,...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
In c++, Can anyone make these code int user input as user have to put all...
In c++, Can anyone make these code int user input as user have to put all the amount. int main()    {        Polygon p1(3,4,4.5,5.5);        Polygon p2(4,4,5.5,6.5);        Polygon p3(5,4,6.5,7.5);               cout<<"Area of Polygon#1:"<<p1.getArea()<<endl;        cout<<"Perimeter of Polygon#1:"<<p1.getPerimeter()<<endl;               cout<<"\nArea of Polygon#2:"<<p2.getArea()<<endl;        cout<<"Perimeter of Polygon#2:"<<p2.getPerimeter()<<endl;               cout<<"\nArea of Polygon#3:"<<p3.getArea()<<endl;        cout<<"Perimeter of Polygon#3:"<<p3.getPerimeter()<<endl;                         return 0;     ...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This function inserts a vehicle into the specified file. • Determine the RBN using the driver's hash function. • Use readRec to read the record at that RBN. • If that location doesn't exist or the record at that location has a szVehicleId[0] == '\0': o Write this new vehicle record at that location using writeRec. • If that record exists and that vehicle's szVehicleId...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT