Personally, I would determine the type of value that was case insensitive and which was divided by the string with the least hint. That way I can use standard hash models 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 enter string values into the set:
> g++ pl.cpp > ./a.out apples - >
Change flag safety:
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); } };
Martin york
source share