Use the transform function to convert the case sensitivity of string

Source: Internet
Author: User

First, let's take a look at the user manual of the transform function:

Template <class inputiterator, class outputiterator, class unaryoperator> <br/> outputiterator transform (inputiterator first1, inputiterator last1, <br/> outputiterator result, unaryoperator OP ); <br/> template <class inputiterator1, class inputiterator2, <br/> class outputiterator, class binaryoperator> <br/> outputiterator transform (inputiterator1 first1, extends last1, <br/> inputiterator2 first2, outputiterator result, <br/> binaryoperator binary_op );

If you have learned the template function, you can easily understand the above APIs. I will not go into details here. Let's take a look at an example:

# Include <iostream> # include <algorithm> # include <string> using namespace STD; int main () {string STR = "Hello world! "; Transform (Str. Begin (), str. End (), str. Begin (), tolower); cout <STR <Endl ;}
Compiling and running of this program in vs is normal, but compiling in G ++ is not the case. The purpose of writing this blog today is to solve the errors.
Compile the source file:
G ++-O test. CC
The following error message is displayed:
Y @ fify-laptop :~ /Desktop $ g ++-o test. cctest. cc: In function 'int main () ': test. cc: 9: error: 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 >>,< unresolved overloaded function type> )'
The last two sentences show that there is an error in the overload function. Ask Google and find that the tolower function has two definitions:
Int std: toupper (int); // from <cctype>
And
Template <class chart> charT std: toupper (charT, const locale &); // from <locale>
The compiler cannot distinguish two functions, resulting in compilation errors. Now let's tell the Compiler which function to use. Let's look at the example:
# Include <iostream> # include <algorithm> # include <string> using namespace std; int main () {string str = "Hello World! "; Transform (str. begin (), str. end (), str. begin (), (int (*) (int) tolower); cout <str <endl ;}
Where (int (*) (int) is the function pointer type, telling the compiler to call the function that "the input parameter is an int and the return value is also an int. If you do not know how to use function pointers, you can ask Google.
Run test to obtain the following results:
Y @ fify-laptop :~ /Desktop $./testhello world!
Refer:
1. http://www.devx.com/getHelpOn/Article/9702/1954
2. http://www.cplusplus.com/reference/algorithm/transform/

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.