String case-insensitive conversion functions-blog channel-csdn. net
String case Conversion Function Category: C/C ++/C # Read by 1495 Comment (0) Favorites Report Stringbasicalgorithmc multi-thread function
Recently suffering from multithreading and wild pointers ing ......
C ++ does not have a string function to convert the case sensitivity directly. You must implement this function by yourself. Generally, you can use the algorithm of STL to implement:
# Include <iostream>
# Include <cctype>
# Include <string>
# Include <algorithm>
Using namespace STD;
Int main ()
{
String S = "ddkfjsldjl ";
Transform (S. Begin (), S. End (), S. Begin (), toupper );
Cout <S <Endl;
Return 0;
}
However, when compiling with G ++, an error is returned:
For 'transform (_ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>, _ gnu_cxx ::__ normal_iterator <char *, STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>,_ gnu_cxx: :__ normal_iterator <char *, STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>, <unresolved overloaded function type>).
The error occurs because Linux implements toupper as a macro instead of a function.:
/Usr/lib/syslinux/com32/include/ctype. h:
/* Note: This is decimal, not hex, to avoid accidental promotion to unsigned */
# DEFINE _ toupper (_ C )&~ 32)
# DEFINE _ tolower (_ C) | 32)
_ Ctype_inline int toupper (INT _ C)
{
Return islower (_ C )? _ Toupper (_ C): _ C;
}
_ Ctype_inline int tolower (INT _ C)
{
Return isupper (_ C )? _ Tolower (_ C): _ C;
}
Two solutions:
1.Transform (Str. Begin (), str. End (), str. Begin (), (INT (*) (INT) toupper );
Here (INT (*) (INT) toupper converts toupper into a function pointer with a return value of Int. The parameter has only one Int.
2. Implement the toupper function by yourself:
Int toupper (int c)
{
Return toupper (C );
}
Transform (Str. Begin (), str. End (), str. Begin (), toupper );
Appendix: case-sensitive Conversion Function
# Include <cctype>
# Include <string>
# Include <algorithm>
Using namespace STD;
Void toupperstring (string & Str)
{
Transform (Str. Begin (), str. End (), str. Begin (), (INT (*) (INT) toupper );
}
Void tolowerstring (string & Str)
{
Transform (Str. Begin (), str. End (), str. Begin (), (INT (*) (INT) tolower );
}