I am comparing two strings, str1 and str2, using the string.compare function in #string. Is there a way to force the class to think that '-' is the equivalent of ' '. Looking at the member functions of char_traits I thought that the .assign would allow me to accomplish this but it is acting as if I am saying, str1='-'; or str1=' ';. I would prefer not to rewrite my own string handling class.
I am comparing two strings, str1 and str2, using the string.compare function in #string.
Share
There are several possibilities, depending whether you want this behavior:
If you want to behavior once: simply use your own (custom) algorithm:
If this behavior need be encoded within the class itself, you need to use
basic_stringand provide a custom trait class.The traits class provides a
static int compare ( const char_type* s1, const char_type* s2, size_t n);function that is used bystd::string::compareunder the hood.So for example:
Of course,
MyStringis then completely incompatible with otherstd::string.Frankly, if you can, simply “normalize” your string and decide whether you’ll use ‘-‘ or ‘ ‘. It will make your life easier.