?
Solved

change compare criterion for map

Posted on 2002-04-14
3
Medium Priority
?
276 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:mfc_speak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 200 total points
ID: 6941334
struct costGreater
{
    bool operator()(const string str1, const string str2) const
    {
        return  stricmp(str1.c_str(), str2.c_str()) > 0 ?  true : false;
    }
};


map<string,STRUCT, costGreater> mymap;

=========================

Demo program

#if (_MSC_VER >= 1200)
#pragma warning(disable: 4786)
#endif

#include <string.h>
#include <string>
#include <map>
#include <iostream>
using namespace std;

struct costGreater
{
    bool operator()(const string str1, const string str2) const
    {
        return  stricmp(str1.c_str(), str2.c_str()) > 0 ?  true : false;
    }
};

int main()
{
    map<string,int, costGreater>  mymap;

    mymap["hello"] = 1;
    mymap["HELLO"] = 20;

    cout << mymap["hello"] << endl << mymap["HELLO"];

    return 0;
}


0
 
LVL 30

Expert Comment

by:Axter
ID: 6941880
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_
0
 

Author Comment

by:mfc_speak
ID: 6942810
Simple and works
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question