De lo que he leído esto es más portátil que stricmp() porque stricmp() no es, de hecho, parte de la biblioteca std , pero solo implementado por la mayoría de los proveedores de compiladores. Como resultado a continuación, mi solución es simplemente la suya.
#include <string>
#include <cctype>
#include <iostream>
#include <set>
struct caseInsensitiveLess
{
bool operator()(const std::string& x, const std::string& y)
{
unsigned int xs (x.size());
unsigned int ys (y.size());
unsigned int bound (0);
if (xs < ys)
bound = xs;
else
bound = ys;
{
unsigned int i = 0;
for (auto it1 = x.begin(), it2 = y.begin(); i < bound; ++i, ++it1, ++it2)
{
if (tolower(*it1) < tolower(*it2))
return true;
if (tolower(*it2) < tolower(*it1))
return false;
}
}
return false;
}
};
int main()
{
std::set<std::string, caseInsensitiveLess> ss1;
std::set<std::string> ss2;
ss1.insert("This is the first string");
ss1.insert("THIS IS THE FIRST STRING");
ss1.insert("THIS IS THE SECOND STRING");
ss1.insert("This IS THE SECOND STRING");
ss1.insert("This IS THE Third");
ss2.insert("this is the first string");
ss2.insert("this is the first string");
ss2.insert("this is the second string");
ss2.insert("this is the second string");
ss2.insert("this is the third");
for (auto& i: ss1)
std::cout << i << std::endl;
std::cout << std::endl;
for (auto& i: ss2)
std::cout << i << std::endl;
}
de salida con el caso insensible conjunto y conjunto regular que muestra la misma pedido:
This is the first string
THIS IS THE SECOND STRING
This IS THE Third
this is the first string
this is the second string
this is the third
Puede aclarar un poco lo que se entiende por 'caso inserción sensible'? – Jon