Thread: Making a CString map case insensitive

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Making a CString map case insensitive

    Hello,

    I've tried to figure this out, but so far I haven't succeeded. I have a program that uses CStrings, and at one point there's a definition of a map: std::map<CString, CUser>
    Somewhere else in the program the map is being iterated. I want to make the map case insensitive so it will output every CString in the correct lexicographic order, no matter what case it's in.
    So when definining the map I now put std::map<CString, CUser, class Cmp = NoCase<CString>>. However I don't know how to define a lesser than compare operator for CStrings.

    I never used C-style strings before so I'm in the need of some help on how to solve this.
    Thanks a lot if you can help me out,

    Edward.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look up CString::CompareNoCase() at msdn or in your helpfiles. BTW this qualifies as windows programming so for mfc questions use the windows forum.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    However I don't know how to define a lesser than compare operator for CStrings.
    Hi,

    A comparison function for strings starts at the beginning of each string and examines each character's ascii code. What you could do is use tolower() to change each character in a string to lower case, and then use the comparison functions that are already defined for strings, e.g strcmp() for cstrings or <, =, or > for C++ string's.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Here is one I used:
    Code:
    namespace
    {
     struct CStringCompareNoCaseLessThan
     {
      bool operator()(const CString& lhs, const CString& rhs) const
      {
       return lhs.CompareNoCase(rhs) < 0;
      }
     };
    };
     
    typedef std::map<CString, CUser, CStringCompareNoCaseLessThan> mapNameToUser;

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I'd suggest a class template. Override the < 'z' or > 'A' operators. Take my suggestion as incomplete. I need a cigaret.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM