Link to home
Start Free TrialLog in
Avatar of mfc_speak
mfc_speak

asked on

change compare criterion for map

I have
map<string,STRUCT> mymap;

When I do a find on the key, I get case sensitive comparison. How do I get a case-insensitive comparison? I think I should change the default 'comp' function.
ASKER CERTIFIED SOLUTION
Avatar of thienpnguyen
thienpnguyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Axter
stricmp is not part of the C++ standard, although most compilers do support it.

Here's the code for a case insensitive string. (ci_string)

#ifndef CI_STRING_HEADER_GAURD_
#define CI_STRING_HEADER_GAURD_

#include <string>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <functional>

/* replace functions of the standard char_traits<T_chr>
* so that strings behave in a case-insensitive way
*/
template <typename T_chr>
struct ci_char_traits : public std::char_traits<T_chr> {
    // return whether c1 and c2 are equal
    static bool eq(T_chr c1, T_chr c2)
     {
        return toupper(c1)==toupper(c2);
    }
     static bool ne( T_chr c1, T_chr c2 )
     {
          return toupper(c1) != toupper(c2);
     }
     
    // return whether c1 is less than c2
    static bool lt(T_chr c1, T_chr c2)
     {
        return toupper(c1)<toupper(c2);
    }
    // compare up to n characters of s1 and s2
    static int compare(const T_chr* s1, const T_chr* s2, size_t n)
     {
        for (size_t i=0; i<n; ++i)
          {
            if (!eq(s1[i],s2[i]))
               {
                return lt(s1[i],s2[i])?-1:1;
            }
        }
        return 0;
    }
    // search c in s
    static const T_chr* find(const T_chr* s, size_t n, T_chr c)
     {
        while( n-- > 0 && toupper(*s) != toupper(c) )
          {
               ++s;
          }
          return s;
         
    }
};

// define a special type for such strings
typedef std::basic_string<char,ci_char_traits<char> > ci_string;
typedef std::basic_string<wchar_t,ci_char_traits<wchar_t> > wci_string;

/* define an output operator
* because the traits type is different than that for std::ostream
*/
inline std::ostream& operator << (std::ostream& strm, const ci_string& s)
{
    // simply convert the ci_string into a normal string
    return strm << std::string(s.data(),s.length());
}

inline std::ostream& operator << (std::ostream& strm, const wci_string& s)
{
    // simply convert the ci_string into a normal string
    return strm << std::wstring(s.data(),s.length()).c_str();
}
#endif //!CI_STRING_HEADER_GAURD_
Avatar of mfc_speak
mfc_speak

ASKER

Simple and works