una que programa primera muestre minusculas mayusculas mayuscula letra lenguaje identificar hasta escribir diferenciar convierte convertir contar como abecedario c++ stl

c++ - primera - escribir un programa que muestre el abecedario en minusculas, de la "a" hasta la "z".



Distintivo de mayúsculas y minúsculas: conjunto de cadenas (3)

Necesita definir un comparador personalizado:

struct InsensitiveCompare { bool operator() (const std::string& a, const std::string& b) const { return stricmp(a.c_str(), b.c_str()) < 0; } }; std::set<std::string, InsensitiveCompare> s;

¿Cómo se tiene una inserción insensible a mayúsculas y minúsculas o una cadena de búsqueda en std :: set?

Por ejemplo-

std::set<std::string> s; s.insert("Hello"); s.insert("HELLO"); //not allowed, string already exists.


std :: set ofrece la posibilidad de proporcionar su propio comparador (al igual que la mayoría de los contenedores estándar). A continuación, puede realizar cualquier tipo de comparación que desee. El ejemplo completo está disponible aquí


Por lo que he leído, esto es más portable que stricmp () porque stricmp () no es de hecho parte de la biblioteca std, sino que solo es 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; }

Salida con conjunto insensible a mayúsculas / minúsculas y conjunto regular que muestra el mismo orden:

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