Update
I should have said I am not allowed to use the string class I am making my own. So I can't use things like strcmp. |
Do not "update" your thread by editing
editing the starting
thread post, because it won't show up for us. Bump it with a reply.
The libraries names can be a bit confusing. In particular
string.h which contains
strcmp() is not the same as
string which contains
std::string.
Moreover, in C++
string.h has been renamed to
cstring, with the former deprecated. Whether or not this was done just for the sake of incompatibility is a different topic.
Anyway, if you are
explicitly forbidden from using
std::strcmp(), which is stupid of your teachers, you can respond with equal stupidity by writing what is basically
strcmp() yourself.
Just to be clear, I'm not calling you stupid, I'm just saying this restriction is stupid and therefore the "solution" will be stupid as well.
Disclaimer: the code below was not tested so it may contain bugs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <cstddef>
bool String::operator < (const String &rhs) const
{
std::size_t i=0; // for clarity, prefer using this type for sizes and lengths
// '\0' is the NUL character
// we could write a plain 0 instead, but we emphasize that it's a char
while (ch[i] != '\0' && rhs.ch[i] != '\0')
if (ch[i] == rhs.ch[i]) // they're equal, move up
++i;
else
break; // they're not equal, break the loop, then return
return ch[i] < rhs.ch[i];
}
|
Edit: mistakes, and to dedicate this post to Vlad.