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

nomasp發表於2015-05-22

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 line
for(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 returns
decltype(s.size()) punct_cnt= 0;
// count the number of punctuation charaters in s
for(auto c: s)  // for every char in s
    if(ispunct(c))  // if the character is punctuation
        ++punct_cnt;  // increment the punctuation counter
cout<< 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 digits
cout<< "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 string
string::size_type n;  // hold numbers from the input
while(cin>>n)
    if(n < hexdigits.size())  // ignore invalid input
        result+= hexdigits[n];  // fetch the indicated hex digit
cout<< "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.

相關文章