today, I wrote a small example of converting all lowercase letters in a string into uppercase letters.
http://blog.csdn.net/yasaken/article/details/7303903
1#include"stdafx.h"2#include <string>3#include <algorithm>4#include <iostream>5 6 using namespacestd;7 8 int_tmain (intARGC, _tchar*argv[])9 {Ten stringStra ="[email protected] $ABC"; One transform (Stra.begin (), Stra.end (), Stra.begin (),:: ToUpper);///I didn't understand why the ToUpper was preceded by: Acout<<stra<<Endl; - GetChar (); - return 0; the}
If there is no above using namespace std; This sentence
Transform (Stra.begin (), Stra.end (), Stra.begin (),:: ToUpper), and transform (Stra.begin (), Stra.end (), Stra.begin (), ToUpper); Can all be compiled by
From the Internet to find an example of the explanation http://blog.sina.com.cn/s/blog_48d5933f0100riz5.html
The standard library overloads a ToUpper function, and GCC is fully overloaded with the C library, and glibc does not, so g++ at compile time thinks the function is ambiguous. Here are two forms of toupper functions in the standard library
int std::toupper (int); From <cctype>
Chart Std::toupper (chart, const local&); From <locale>
There are three ways to resolve this:
The first wrapper function--only the function that you want to use in the wrapper function, the ambiguity is gone.
int toUpper (int c)
{
Return ToUpper (c);
}
The second type of forced conversion---> converts a toupper to a function pointer with a return value of int, with only an int argument
Std::transform (S.begin (), S.end (), S.begin (), (Int (*) (int)) toupper);
The third kind of GCC implements ToUpper as a macro instead of a function, and there are implemented functions (not macros) in the global namespace, so we explicitly namespace, which doesn't always work, but I don't have this problem in my g++ environment.
Transform (S.begin (), S.end (), S.begin (),:: ToUpper);
and a small example.
Now let's take a look at the transform function, and the developer just needs to provide a function object, such as a toupper function that turns char into uppercase or a lowercase function tolower function
class class class Unaryoperator > class class class Binaryoperator > outputiterator Transform (InputIterator1 first1, InputIterator2 last1, InputIterator2 first2, Outputiterator result, Binaryoperator binary_op);
The
Example of turning all lowercase letters of a string into uppercase letters looks at the use of global functions