Hello,
In short, I need to compare strings using the '<' operator. Ex: " if (foo< bar) std::cout << "foo is less than bar\n"; "
The way I understand it from "
http://www.cplusplus.com/reference/string/string/operators/"
both the left-hand-side and the rhs of the operator have to be const string&
"bool operator< (const string& lhs, const string& rhs);
bool operator< (const char* lhs, const string& rhs);
bool operator< (const string& lhs, const char* rhs);"
In my code, I have a string value that is already a const string&, and a string value that is a string*.
The problem is, when I try to compare a const string& to a string*, I get an error.
I am new to this and barely understand what a const string& is, and why I cannot compare it to a string*.
Could you please help me in finding a way to compare these two strings for my BST insertion?
Here is my BST class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class BST
{
public:
BST();
~BST();
void insertContent(const string& word, const string& definition);
void deleteContent(string* word);
const string* getContent(const string& word);
private:
class Node
{
public:
Node(string* word, string* definition)
{left=NULL; right=NULL; m_word=word; m_definition=definition;}
Node* left;
Node* right;
string* m_word;
string* m_definition;
};
|
Here is the insert function where I need help comparing strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void BST::insertContent(const string& word, const string& definition)
{
Node* ptr = root;
//Node* entry = new Node(word, definition);
if (root == NULL)
{
root = new Node(word, definition);
return;
}
while (ptr != NULL)
{
const string& curwor = ptr->m_word; /*I was thinking of making my own const string& out of ptr->m_word but it didn't work. */
if (word < ptr->m_word)
{
}
}
}
|