【C++注意事项】6 Library string Type

Processing Every Character? Use Range-Based for

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 for. 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 strcout<< 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;(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

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”:

<< “Enter a series of numbers between 0 and 15″<< ” separated by spaces. Hit ENTER when finished: “<< endl;(cin>>n)if(n < hexdigits.size()) // ignore invalid inputresult+= 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 should 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.

,那段雨骤风狂。人生之旅本就是风雨兼程,是要说曾经拥有,

【C++注意事项】6 Library string Type

相关文章:

你感兴趣的文章:

标签云: