D'après ce que j'ai lu ce qui est plus portable que stricmp() parce stricmp() est en fait pas partie de la bibliothèque std , mais seulement mis en œuvre par la plupart des fournisseurs de compilateurs. Comme résultat ci-dessous est ma solution pour rouler le vôtre.
#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;
}
sortie avec insensible à la casse et FIXER réguliers montrant la même commande:
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
Pouvez-vous clarifier un peu ce que l'on entend par « cas d'insertion sensible »? – Jon