[C ++ Note] 6 Library string Type, librarystring

Source: Internet
Author: User

[C ++ Note] 6 Library string Type, librarystring
Processing Every Character? Use Range-Based

If we want to do something to every character in a string, by far the best approach is to use a statement introduced by the new standard: the range for statement. this statement iterates through the elements in a given sequence and performs some operation on each value in that sequence. the syntactic form is

for( declaration: expression)    statement

Where expression is an object of a type that represents sequence, and declaration defines the variable that we'll use to access the underlying elements in the sequence. on each iteration, the variable in declaration is initialized from the value of the next element in expression.

A string represents a sequence of characters, so we can use a string as the expression in a range. as a simple example, we can use a range for to print each character from a string on its own line of output:

string str("some string");// print the characters in str one character to a linefor(auto c: str)  // for every char in str    cout<< c << endl;  // print the current character followed by a newline

The for loop associates the variable c with str. we define the loop control variable the same way we do any other variable. in this case, we use auto to let the compiler determine the type of c, which in this case will be char. on each iteration, the next character in str will be copied into c. thus, we can read this loop as saying, "For every character c in the string str," do something. the "something" in this case is to print the character followed by a newline.

As a somewhat more complicated example, we'll use a range for and the inpunct function to count the number of punctuation characters in a string:

string s("Hello World!!!");// punct_cnt has the same type that s.size returnsdecltype(s.size()) punct_cnt= 0;// count the number of punctuation charaters in sfor(auto c: s)  // for every char in s    if(ispunct(c))  // if the character is punctuation        ++punct_cnt;  // increment the punctuation countercout<< punct_cnt    << " punctuation characters in "<< s << endl;

The output of this program is

3 puctuation characters in Hello World!!!

Here we use decltype (if you want more message, just go to there: The auto and decltype Type Specifier) to declare our counter.

Using s Subscript for Random Access

In the previous example we advanced our subscript one position at a time to capitalize each character in sequence. we can also calculate an subscript and directly fetch the indicated character. there is no need to access characters in sequence.

As an example, let's assume we have a number between 0 and 15 and we want to generate the hexadecimal representation of that number. we can do so using a string that is initialized to hold the 16 hexadecimal "digits ":

const string hexdigits= "0123456789ABCDEF";  // possible hex digitscout<< "Enter a series of numbers between 0 and 15"    << " separated by spaces. Hit ENTER when finished: "    << endl;string result;  // will hold the  resulting hexify'd stringstring::size_type n;  // hold numbers from the inputwhile(cin>>n)    if(n < hexdigits.size())  // ignore invalid input        result+= hexdigits[n];  // fetch the indicated hex digitcout<< "You hex number is: "<< result << endl; 

If we give this program the input

12 0 5 15 8 15

The output will be

Your hex number is: C05F8F

Whenever we use a subscript, we shoshould think about how we know that it is in range. in this program, our subscript, n, is a string: size_type, which as we know is an unsigned type. as a result, we know that n is guaranteed to be greater than or equal to 0. before we use n to subscript hexdigits, we verify that it is less than the size of hexdigits.

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.