Casing conversion of C + + string

Source: Internet
Author: User

Converting a string to uppercase or lowercase is something you often need to do in a project, but this method is not available in the string class. Write a function to achieve, it is very simple to say, but it always makes people feel inconvenient. For example: In the morning to eat a hamburger, the refrigerator has beef, flour, there is a microwave oven, you can do it yourself, but it is too much trouble, not a few people are willing to do. However, make a call to KFC home emergency delivery, 10 minutes after the hot hamburger delivered to the door, greatly saving time (time is money, you can use the time for more important development work), and taste is not bad, why not?

STL's algorithm library does provide us with the convenience of using the template function transform to easily solve this problem, and developers just need to provide a function object, such as a toupper function that turns char into uppercase or a lowercase function tolower function.


Transform prototypes:

Template <classInputiterator,classOutputiterator,classUnaryoperator >outputiterator Transform (inputiterator first1, Inputiterator last1, Output            Iterator result, Unaryoperator op); Template<classInputIterator1,classInputIterator2,classOutputiterator,classBinaryoperator >outputiterator Transform (InputIterator1 first1, InputIterator1 last1, Inpu  TIterator2 First2, outputiterator result, Binaryoperator binary_op); 

Test code:

1#include <string>2#include <algorithm>3 using namespacestd; 4       5 intMain ()6 {  7         stringStra ="[email protected]"; 8         stringStrB ="[email protected]"; 9printf"before transform:\n"); Tenprintf"stra:%s \ n", Stra.c_str ());  Oneprintf"strb:%s \ n", Strb.c_str ());  A        - transform (Stra.begin (), Stra.end (), Stra.begin (),:: ToUpper);  - transform (Strb.begin (), Strb.end (), Strb.begin (),:: ToUpper);  theprintf"After transform to toupper:\n");  -printf"stra:%s \ n", Stra.c_str ());  -printf"strb:%s \ n", Strb.c_str ());  -        + transform (Stra.begin (), Stra.end (), Stra.begin (),:: ToLower);  - transform (Strb.begin (), Strb.end (), Strb.begin (),:: ToLower);  +printf"After transform to lower:\n");  Aprintf"stra:%s \ n", Stra.c_str ());  atprintf"strb:%s \ n", Strb.c_str ());  -         return 0;  -}

Operation Result:

1Stra:[email protected]126. com2 Strb:[email protected]3   4 After transform to ToUpper:5Stra:[email protected]126. COM6 Strb:[email protected]7   8 After transform to lower:9Stra:[email protected]126. comTenStrb:[email protected]
Reprinted from: http://blog.csdn.net/zhongyhc/article/details/41686893.

Casing conversion of C + + string

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.