设为首页 加入收藏

TOP

3.2 Library string Type (6)
2013-10-07 16:19:03 来源: 作者: 【 】 浏览:74
Tags:3.2 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

  1. for (declaration : expression)   
  2. statement  

where expression is an object of a type that represents a 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:

  1. string str("some string");   
  2. // print the characters in str one character to a line   
  3. for (auto c : str) // for every char in str   
  4. 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 (§ 2.5.2, p. 68) 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 ispunct function to count the number of punctuation characters in a string:

  1. string s("Hello World!!!");   
  2. // punct_cnt has the same type that s.size returns; see § 2.5.3 (p. 70)   
  3. decltype(s.size()) punct_cnt = 0;   
  4. // count the number of punctuation characters in s   
  5. for (auto c : s) // for every char in s   
  6. if (ispunct(c)) // if the character is punctuation   
  7. ++punct_cnt; // increment the punctuation counter   
  8. cout << punct_cnt   
  9. << " punctuation characters in " << s << endl;  

The output of this program is

  1. 3 punctuation characters in Hello World!!!  

Here we use decltype (§ 2.5.3, p. 70) to declare our counter, punct_cnt. Its type is the type returned by calling s.size, which is string::size_type. We use a range for to process each character in the string. This time we check whether each character is punctuation. If so, we use the increment operator (§ 1.4.1, p. 12) to add 1 to the counter. When the range for completes, we print the result.


Using a Range for to Change the Characters in a string

If we want to change the value of the characters in a string, we must define the loop variable as a reference type (§ 2.3.1, p. 50). Remember that a reference is just another name for a given object. When we use a reference as our control variable, that variable is bound to each element in the sequence in turn. Using the reference, we can change the character to which the reference is bound.

Suppose that instead of counting punctuation, we wanted to convert a string to all uppercase letters. To do so we can use the library toupper function, which takes a character and returns the uppercase version of that character. To convert the whole string we need to call toupper on each character and put the result back in that character:

  1. string s("Hello World!!!");   
  2. // convert s to uppercase   
  3. for (auto &c : s) // for every char in s (note: c is a reference)   
  4. c = toupper(c); // c is a reference, so the assignment changes the char in s   
  5. cout << s << endl;  

The output of this code is

  1. HELLO WORLD!!!  

On each iteration, c refers to the next character in s. When we assign to c, we are changing the underlying character in s. So, when we execute

  1. c = toupper(c); // c is a reference, so the assignment changes the char in s  

we’re changing the value of the character to which c is bound. When this loop completes, all the characters in str will be uppercase.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇3.2 Library string Type (5) 下一篇3.2 Library string Type (7)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·用 Python 进行数据 (2025-12-25 15:49:09)
·如何学习Python数据 (2025-12-25 15:49:07)
·利用Python进行数据 (2025-12-25 15:49:04)
·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)