| | |
Type comparable
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
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:
Any input or help is appreciated, I'm new to c++
C++ Syntax (Toggle Plain Text)
#ifndef index_h #define index_h #include <String> using namespace std; class index{ public: index(string word); void putpage(int number); bool haspage(int number); }; #endif
Any input or help is appreciated, I'm new to c++
write an == operator
C++ Syntax (Toggle Plain Text)
class index{ public: index(string word); void putpage(int number); bool haspage(int number); bool operator==(index& idx); // compare the two class objects };
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.
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
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"
Again, any input is greatly appreciated.
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"
C++ Syntax (Toggle Plain Text)
#ifndef structure_h #define structure_h #include <vector> #include <String> #include "BinarySearchTree.h" using namespace std; template<class Comparable> class BinarySearchTree; template<class Comparable> 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); // compare the two class objects friend class BinarySearchTree<Comparable>; }; #endif
Again, any input is greatly appreciated.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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
Comparable is just a place holder; the above is equivalent to
and your structure could be
this is how you could instantiate a BinarySearchTree of structures.
and this would declare a function taking a binary search tree of structures as an argument
nothing at all; just declare and implement the comparison operator(s). in
C++ Syntax (Toggle Plain Text)
template<class Comparable> class BinarySearchTree;
C++ Syntax (Toggle Plain Text)
template< class T > 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.
C++ Syntax (Toggle Plain Text)
BinarySearchTree<structure> the_search_tree ;
and this would declare a function taking a binary search tree of structures as an argument
C++ Syntax (Toggle Plain Text)
void function( BinarySearchTree<structure>& tree ) ;
Last edited by vijayan121; Feb 27th, 2008 at 3:22 pm.
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
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:
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.
C++ Syntax (Toggle Plain Text)
structure::structure(){ string _word; vector<int> *_indices(); } //template <class Comparable> structure::structure(string word){ string _word; vector<int> *_indices(); } //template <class Comparable> structure::structure(string word, int number){ string _word; vector<int> *_indices(); } //template <class Comparable> void structure::putpage(int number){ *_indices->push_back(number); } //template <class Comparable> bool structure::haspage(int number){ vector<int>::iterator *iter; *iter=(_indices).begin(); /* for(*iter=*_indices->begin(); *iter!=*_indices->end();++*iter){ if(*iter == number){ return true; } }*/ 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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> I think the issues I'm getting are revolving around pointer issues ...
there are several issues. let us take them one by one.
these lines in the constructor(s) do nothing useful.
both the string and the vector have default constructors which would initialize them correctly. so the default constructor needs to be just
in a constructor, we could also initialize a member in a non-default way. eg.
to add a number to the vector indices
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
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.
C++ Syntax (Toggle Plain Text)
{ // creates a string which is destroyed on return string _word; // this merely declares a function named _indices vector<int> *_indices(); }
both the string and the vector have default constructors which would initialize them correctly. so the default constructor needs to be just
C++ Syntax (Toggle Plain Text)
structure::structure() { /* default initialization for _word, _indices */ }
in a constructor, we could also initialize a member in a non-default way. eg.
C++ Syntax (Toggle Plain Text)
structure::structure( const string& word ) : _word( word ) { // member _word initialized with word // default initialization for _indices }
to add a number to the vector indices
C++ Syntax (Toggle Plain Text)
void structure::putpage( int number ) { _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
C++ Syntax (Toggle Plain Text)
#include <algorithm> bool structure::haspage( int number ) { return std::find( _indices.begin(), _indices.end(), number ) != _indices.end() ; }
![]() |
Similar Threads
- C++ advice (scanf/printf) (C++)
- Pismo G3 rebuild (OS 7 / 8 / 9)
- Blackout shows Net's fragility (Geeks' Lounge)
- Is SEO Unethical? (Search Engine Optimization)
- MySQL or MSSQL (MySQL)
- Processor clock speed project (Motherboards, CPUs and RAM)
- Help with casting (Java)
Other Threads in the C++ Forum
- Previous Thread: checking network connectivity?
- Next Thread: Download Website in C++
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






