Take a look at the use of global functions with the example of turning all lowercase letters of a string into uppercase

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.