Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,209 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,282 people online right now. Registration is fast and FREE... Join Now!




Compare problem

 
Reply to this topicStart new topic

Compare problem, How do I specify the variable to compare?

jdclay
8 Jul, 2008 - 04:09 PM
Post #1

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 3


My Contributions
In my constructor in the preqrec.h header for a process request record object, it accepts two variables, its name and priority. However, I want to create a mini priority queue (a predefined STL) with a comparison statement in the declaration of the queue to make use of my boolean operator< specified in my header file.
I can't find in my book how exactly to compare one variable in a custom object, namely the priority value.

Here's my code
CODE

//preqrec.h

#ifndef PROCESS_REQUEST_RECORD_CLASS
#define PROCESS_REQUEST_RECORD_CLASS

#include <String>
#include <iostream>

   using namespace std;

    class procReqRec
   {
   public:
      procReqRec();
       //default constructor
       
      procReqRec(const string& nm, int p);
       //constructor
       
      int getPriority();
      string getName();
       //access functions
       
      void setPriority(int p);
      void setName(const string& nm);
       //update functions
       
      friend bool operator< (const procReqRec& left, const procReqRec& right);
       //for maintenence of a minimum priority queue
  
      friend ostream& operator<< (ostream& ostr, const procReqRec& obj);
       //output a process request record in the format
           //name: priority
  
   private:
      string name;  //process name
      int priority; //process priority
   };

    procReqRec::procReqRec()
   {
   }

    procReqRec::procReqRec(const string& nm, int p)
   {
      name = nm;
      priority = p;
   }

    int procReqRec::getPriority()
   {
      return priority;
   }
    
    string procReqRec::getName()
   {
      return name;
   }
    
    void procReqRec::setPriority(int p)
   {
      priority = p;
   }
    
    void procReqRec::setName(const string& nm)
   {
      name = nm;
   }
  
    bool operator< (const procReqRec& left, const procReqRec& right)
   {
      return left.priority < right.priority;
   }

    ostream& operator<< (ostream& ostr, const procReqRec& obj)
   {
      return ostr << "Name: "<< obj.name << " Priority: " << obj.priority;
   }

#endif

CODE

//program
#include <String>
#include <iostream>
#include <d_random.h>
#include <d_pqueue.h>
#include <preqrec.h>

   using namespace std;

    int main()
   {
      miniPQ<procReqRec, less<??> > mpq;  //<--This is where I am having issues.
      randomNumber rand;
  
      mpq.push( procReqRec("Process A", rand.random(40)) );
      mpq.push( procReqRec("Process B", rand.random(40)) );
      mpq.push( procReqRec("Process C", rand.random(40)) );
      mpq.push( procReqRec("Process D", rand.random(40)) );
      mpq.push( procReqRec("Process E", rand.random(40)) );
      mpq.push( procReqRec("Process F", rand.random(40)) );
      mpq.push( procReqRec("Process G", rand.random(40)) );
      mpq.push( procReqRec("Process H", rand.random(40)) );
      mpq.push( procReqRec("Process I", rand.random(40)) );
      mpq.push( procReqRec("Process J", rand.random(40)) );
  
      while(!mpq.empty())
      {
         cout<<mpq.top()<< " ";
         mpq.pop();
      }
  
      cout<<endl;
  
      system("pause");
      return EXIT_SUCCESS;
   }

User is offlineProfile CardPM
+Quote Post

captainhampton
RE: Compare Problem
8 Jul, 2008 - 04:27 PM
Post #2

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
You are most likely looking for "Overloading Operators" or something of the sort. Something that will allow you to compare two different types, if you need a boolean "<" operator overload the template would be something like this:

CODE

bool operator>(const Var& x, const Var& y)
{
    //Comparison Code here
        return true;
    return false;
}


This post has been edited by captainhampton: 8 Jul, 2008 - 04:27 PM
User is offlineProfile CardPM
+Quote Post

jdclay
RE: Compare Problem
8 Jul, 2008 - 06:41 PM
Post #3

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 3


My Contributions
QUOTE(captainhampton @ 8 Jul, 2008 - 08:27 PM) *

You are most likely looking for "Overloading Operators" or something of the sort. Something that will allow you to compare two different types, if you need a boolean "<" operator overload the template would be something like this:

CODE

bool operator>(const Var& x, const Var& y)
{
    //Comparison Code here
        return true;
    return false;
}



As you would have seen if you looked through my header file, I already overloaded it:

CODE

bool operator< (const procReqRec& left, const procReqRec& right)
   {
      return left.priority < right.priority;
   }


What I'm asking is how to apply that comparison operator to a specific variable in my object,
CODE
proqRegRec(string nm, int p)
to compare based on the int p from my procReqRec object in my mini priority queue in the compare clause below
CODE
miniPQ<procReqRec, Compare comp > mpq;


Thanks for your input

--J
User is offlineProfile CardPM
+Quote Post

jdclay
RE: Compare Problem
8 Jul, 2008 - 07:01 PM
Post #4

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 3


My Contributions
Nevermind, I was making it too complicated.

It just needed to be:
CODE

miniPQ<procReqRec, less<procReqRec> > mpq;

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month