简体   繁体   中英

Case-Insensitive STL Containers (e.g. std::unordered_set)

What is the shortest, most cross-platform way to make a std::unordered_set CASE-INSENSITIVE container?

my_set.insert("Apples");  
my_set.insert("apples"); //Insert doesn't occur because of duplicate item

I know STL provides Hash and Pred . What should Hash be? What should Pred be? if they are not-builtins then please provide the code for them along with an example of their use (ie how do I declare std::unordered_set ?).

Due to the Criticism I will elaborate on what I am trying to do. I need a high performance transparent HTTP proxy server, one of the things it does is looks up HTTP header fields quickly. HTTP header fields are defined as being case-insensitive so I need a case-insensitive container.

The definition of unordered_set is

  template <class Value,
            class Hash = hash<Value>,
            class Pred = std::equal_to<Value>,
            class Alloc = std::allocator<Value> >
  class unordered_set;

If you provide Hash and Pred functors that are case-insensitive, then the set will become so too.

This is a simple example, the string hash function is simplisti c but you can change it to your needs

struct MyHash
{
    size_t operator()(const std::string& Keyval) const
    {
        //You might need a better hash function than this
        size_t h = 0;
        std::for_each( Keyval.begin() , Keyval.end() , [&](char c )
        {
            h += tolower(c);
        });
        return h;
    }
};

struct MyEqual
{
    bool operator()(const std::string& Left, const std::string& Right) const
    {
        return Left.size() == Right.size() 
             && std::equal ( Left.begin() , Left.end() , Right.begin() ,
            []( char a , char b )
        {
            return tolower(a) == tolower(b); 
        }
        );
    }
};


int main()
{
    std::unordered_set< std::string , MyHash , MyEqual > m;

    m.insert( "Apple" );
    m.insert( "apple" );

    return 0;
}

Personally i would define a value type that was case insensitive and that devolves into a string at the merest hint. Thus allowing me to use the standard hash and predicate models.

#include <string>
#include <unordered_set>
#include <iostream>
#include <algorithm>
#include <iterator>

class LCString
{
    std::string  data;

    public:
        operator std::string&()             {return data;}
        operator std::string const&() const {return data;}

        LCString(char const* init)
        {
            std::transform(init, init + strlen(init),
                           std::back_inserter(data), &::tolower);
        }
};

int main()
{
    typedef std::unordered_set<LCString, 
                               std::hash<std::string>, 
                               std::equal_to<std::string> >  MySet;
    MySet  data;
    data.insert("Apples");
    data.insert("apples");

    std::copy(data.begin(), data.end(),
              std::ostream_iterator<std::string>(std::cout, " - "));
    std::cout << "\n";
}

Thus we only put lowercase values into the set:

> g++ pl.cpp
> ./a.out
apples -
>

Edit Case Preserving:

class LCStringOriginalPreserved
{
    std::string  original;
    std::string  data;

    public:
        operator std::string&()             {return data;}
        operator std::string const&() const {return data;}

        std::string& getOriginal()          {return original;}

        LCString(char const* init)
          : original(init)
        {
            std::transform(original.begin(), original.end(),
                           std::back_inserter(data), &::tolower);
        }
};

I like this better.

Works on Linux.

#include <strings.h>
#include <ctype.h>

#include <string>
#include <functional>
#include <tr1/functional_hash.h>

struct iequal_to : public std::binary_function <std::string,std::string,bool>
{
  bool operator() (const std::string& x, const std::string& y) const
  {
    return (!strcasecmp(x.c_str(), y.c_str()));
  }
};

const std::string LC(const std::string& x)
{
  std::string ret(x);
  std::string::size_type i;
  for(i = 0; i < x.size(); ++i)
    ret[i] = tolower(x[i]);
  return ret;
}

struct ihash : public std::unary_function <std::string,size_t>
{
  size_t ihash::operator() (const std::string& x) const
  {
    return std::tr1::hash<std::string>()(LC(x));
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM