It is very difficult. All you need to write your own feature class, in particular, you need to extract it from the char_traits<> class template and override the eq () and compare () function (Note: only overriding eq () will not work, even if not changes to override compare () , you have to write it to your derived class as such!). Suppose this class class is sequence_traits and calls your custom string sequence . After all, a string is a sequence of characters!
Note. . I understand from your message that you want alphabets[i] == alphabets[25-i] be considered the same, this means that the first letter and last letter are the same, the second letter and second last letter are the same, and therefore on the!
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 you can do this typedef for convenience:
typedef basic_string<char, sequence_traits> sequence;
You are done. Now you can use sequence . sequence
Working example: http://www.ideone.com/ByBRV
Read this article to find out how it works in detail: http://www.gotw.ca/gotw/029.htm
source share