[C + +] uses a range-based for loop action string

Source: Internet
Author: User

C++11 provides a scope for statement that iterates through each element in a given sequence and performs some action on each element in the sequence:

forexpression)    statement
    • Output each character in a string:
    string str("some string");    for (auto c : str)    {        cout << c << endl;    }
在for循环中使用auto声明变量c,由编译器决定其类型,每次循环,将str中的下一个字符拷贝到c中。
    • Use the ISPUNCT function to count the number of punctuation in a string
    string s("Hello World!!!");    decltype0;    for (auto c : s)    {        if (ispunct(c))            ++punct_cnt;    }    cout" punctuation characters in " << s << endl;
    • Use the range for statement to change the character of a string
    for (auto &c : s)    {        toupper(c);    }    cout << s << endl;
c是string s中字符的引用,使用toupper将string中字符改成大写字符。

[C + +] uses a range-based for loop action string

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.