Type comparable

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 9
Reputation: jrkeller27 is an unknown quantity at this point 
Solved Threads: 0
jrkeller27 jrkeller27 is offline Offline
Newbie Poster

Type comparable

 
0
  #1
Feb 26th, 2008
In C++ is there a way to define a class to be "comparable"? What Im trying to do is write a class that holds a string and a vector of numbers. If an object of this type is compared to another object I want the string that each object contains to be the "thing" thats compared. The header of my class so far is:

  1. #ifndef index_h
  2. #define index_h
  3. #include <String>
  4. using namespace std;
  5. class index{
  6.  
  7. public:
  8. index(string word);
  9. void putpage(int number);
  10. bool haspage(int number);
  11.  
  12.  
  13. };
  14. #endif

Any input or help is appreciated, I'm new to c++
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Type comparable

 
0
  #2
Feb 26th, 2008
write an == operator
  1. class index{
  2.  
  3. public:
  4. index(string word);
  5. void putpage(int number);
  6. bool haspage(int number);
  7. bool operator==(index& idx); // compare the two class objects
  8.  
  9. };
Last edited by Ancient Dragon; Feb 26th, 2008 at 6:56 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Type comparable

 
2
  #3
Feb 26th, 2008
You can also write operator< , operator<= , etc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: jrkeller27 is an unknown quantity at this point 
Solved Threads: 0
jrkeller27 jrkeller27 is offline Offline
Newbie Poster

Re: Type comparable

 
0
  #4
Feb 27th, 2008
Okay I have overloaded those operators and implemented it in my source file. Now my next question is what do I have to do to make this class able to be put into a Binary Search Tree. The BST I'm using does comparisons solely using the operator< so I think I have that set.

What type of syntax do I need to set my class to be of type comparable. Here is what my header file looks like after messing around and trying using template<type Comparable>. Note that I changed the class name to simply "structure"

  1. #ifndef structure_h
  2. #define structure_h
  3. #include <vector>
  4. #include <String>
  5. #include "BinarySearchTree.h"
  6. using namespace std;
  7. template<class Comparable>
  8. class BinarySearchTree;
  9.  
  10. template<class Comparable>
  11. class structure{
  12.  
  13. public:
  14. string _word;
  15. vector<int> _indices;
  16. structure();
  17. structure(string word);
  18. structure(string word, int number);
  19. void putpage(int number);
  20. bool haspage(int number);
  21. bool operator< (const structure & rhs); // compare the two class objects
  22.  
  23. friend class BinarySearchTree<Comparable>;
  24. };
  25.  
  26. #endif

Again, any input is greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Type comparable

 
0
  #5
Feb 27th, 2008
> What type of syntax do I need to set my class to be of type comparable?

nothing at all; just declare and implement the comparison operator(s). in
  1. template<class Comparable>
  2. class BinarySearchTree;
Comparable is just a place holder; the above is equivalent to
  1. template< class T >
  2. class BinarySearchTree;

and your structure could be
class structure
{
public:
  string _word;
  vector<int> _indices;
  structure();
  structure(string word);
  structure(string word, int number);
  void putpage(int number);
  bool haspage(int number);
  bool operator< (const structure & rhs) const ; // this should be a selector

  friend class BinarySearchTree<structure>;
};

this is how you could instantiate a BinarySearchTree of structures.
  1. BinarySearchTree<structure> the_search_tree ;

and this would declare a function taking a binary search tree of structures as an argument
  1. void function( BinarySearchTree<structure>& tree ) ;
Last edited by vijayan121; Feb 27th, 2008 at 3:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: jrkeller27 is an unknown quantity at this point 
Solved Threads: 0
jrkeller27 jrkeller27 is offline Offline
Newbie Poster

Re: Type comparable

 
0
  #6
Feb 27th, 2008
Awesome thanks! Now I'm writing the implementation to the header file and I think the issues I'm getting are revolving around pointer issues, but I'm not sure. The pertinent code is:

  1.  
  2. structure::structure(){
  3. string _word;
  4. vector<int> *_indices();
  5. }
  6. //template <class Comparable>
  7. structure::structure(string word){
  8. string _word;
  9. vector<int> *_indices();
  10.  
  11.  
  12. }
  13.  
  14. //template <class Comparable>
  15. structure::structure(string word, int number){
  16. string _word;
  17. vector<int> *_indices();
  18.  
  19. }
  20.  
  21. //template <class Comparable>
  22. void structure::putpage(int number){
  23. *_indices->push_back(number);
  24. }
  25.  
  26. //template <class Comparable>
  27. bool structure::haspage(int number){
  28. vector<int>::iterator *iter;
  29. *iter=(_indices).begin();
  30. /* for(*iter=*_indices->begin(); *iter!=*_indices->end();++*iter){
  31. if(*iter == number){
  32. return true;
  33. }
  34. }*/
  35. return false;

What I'm trying to do with haspage(int number) is return a bool to see if the number is in the _indices vector. I'm also getting problems with putpage(int number) regarding void values not being ignored. Any help with syntax/pointer problems would be (again) appreciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Type comparable

 
0
  #7
Feb 27th, 2008
> I think the issues I'm getting are revolving around pointer issues ...
there are several issues. let us take them one by one.

class structure
{

    public:
      string _word;
      vector<int> _indices;
      // ...
      structure() ; 

      // passing a string using const reference is more efficient;
      // we avoid the need to make a copy of a string
      structure( const string& word ) ;
      /// ...
      void putpage( int number ) ;
      bool haspage( int number ) ;
      // ....
};

these lines in the constructor(s) do nothing useful.
  1. {
  2. // creates a string which is destroyed on return
  3. string _word;
  4.  
  5. // this merely declares a function named _indices
  6. vector<int> *_indices();
  7. }

both the string and the vector have default constructors which would initialize them correctly. so the default constructor needs to be just
  1. structure::structure()
  2. { /* default initialization for _word, _indices */ }

in a constructor, we could also initialize a member in a non-default way. eg.
  1. structure::structure( const string& word ) : _word( word )
  2. {
  3. // member _word initialized with word
  4. // default initialization for _indices
  5. }

to add a number to the vector indices
  1. void structure::putpage( int number )
  2. { _indices.push_back( number ) ; }

to check weather a number is present in the vector indices, you could iterate through the vector looking for the number, or (easier) use the algorithm std::find
  1. #include <algorithm>
  2. bool structure::haspage( int number )
  3. {
  4. return std::find( _indices.begin(), _indices.end(), number )
  5. != _indices.end() ;
  6. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC