It's very tricky. All you need to write your own traits class, specifically you need to derive it from char_traits<>
class template, and redefine eq() and compare() function (Note: only redefining eq() would not work; even though there is no change in the redefinition of compare(), you've to write it in your derived class as such!). Lets say this traits class sequence_traits
and call your custom string sequence
. After all, string is a sequence of characters!
Note : What I understand from your post that you want alphabets[i] == alphabets[25-i]
to be treated as same, that means, first letter and last letter same, second letter and second last letter same, and so on!
struct sequence_traits : char_traits<char>
{
//'a' and 'z' are equal
//'b' and 'y' are equal
//'c' and 'x' are equal, and so on.
//that implies, 'a' + 'z' == 'b' + 'y' == 'c' + 'x' == 'd' + 'w == so on
//same for upper cases!
static bool eq(const char& left, const char& right)
{
return ( left == right) || (left + right == 'a' + 'z') || ( left + right == 'A' + 'Z') ;
}
static int compare(const char *first1, const char *first2, size_t count)
{
for (; 0 < count; --count, ++first1, ++first2)
if (!eq(*first1, *first2))
return (lt(*first1, *first2) ? -1 : +1);
return (0);
}
};
And then you can do this typedef
for easy use:
typedef basic_string<char, sequence_traits> sequence;
You're done. You can use sequence
now. :-)
Working example : http://www.ideone.com/ByBRV
Read this article to know how it works in detail : http://www.gotw.ca/gotw/029.htm