Go Using ToUpper function in STL transform algorithm

Source: Internet
Author: User
Tags locale

Translated from http://blog.csdn.net/justin12zhu/article/details/5649236

Today we need to implement a lowercase letter to uppercase function, because the incoming parameter is the STL string class, so the first idea is to use the transform algorithm to achieve this function, but error. After home wrote the following test code to see exactly where and how the wrong solution.

#include <iostream><algorithm><cctype>usingnamespace   int main (intChar *argv[]) {    string s ("  HelloWorld");    Transform (S.begin (), S.end (), S.begin (), toupper);    cout<<s<<Endl;     return 0 ;   }

The following is an error message for g++:

No matching function for call to ' 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> >, < Unknown type>) '

As can be seen from the red section above, the Touppe function is considered to be an unknown type of function. But I also have to separate the ToUpper function to test the time of compilation is passed, that is, this function is not a problem, then exactly what is the problem?

After wandering around the internet, I finally found the reason:

The standard library overloads a Touppe 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 the ToUpper function in the standard library:

int int // From <cctype> class chart >charcharconst locale &);  // From <locale>

The problem is found, but there is always a way to solve it. Since the error is because there is ambiguity, so long as the ambiguity can be eliminated.

1, through the intervention packaging function

This is the simplest way, because the wrapper function only one, as long as in the wrapper function to indicate the function to be used, the ambiguity is not natural, in ToUpper as an example, we can use the following a wrapper function:

int int c) {       return  ToUpper (c);}

2. Forced conversion: Convert 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);

3, GCC will implement ToUpper as a macro instead of a function, and in the global namespace has implemented functions (rather than macros), so we explicitly namespace, this does not always work, but in my g++ environment is not a problem:

Transform (S.begin (), S.end (), S.begin (),:: ToUpper);

Go Using ToUpper function in STL transform algorithm

Related Article

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.