设为首页 加入收藏

TOP

3.2 Library string Type (7)
2013-10-07 16:19:00 来源: 作者: 【 】 浏览:71
Tags:3.2 Library string Type

Processing Only Some Characters

Arange for works well when we need to process every character. However, sometimes we need to access only a single character or to access characters until some condition is reached. For example, we might want to capitalize only the first character or only the first word in a string.

There are two ways to access individual characters in a string: We can use a subscript or an iterator. We’ll have more to say about iterators in § 3.4 (p. 106) and in Chapter 9.

The subscript operator (the [] operator) takes a string::size_type (§ 3.2.2, p. 88) value that denotes the position of the character we want to access. The operator returns a reference to the character at the given position.

Subscripts for strings start at zero; if s is a string with at least two characters, then s[0] is the first character, s[1] is the second, and the last character is in s[s.size() - 1].

The values we use to subscript a string must be >= 0 and < size().

The result of using an index outside this range is undefined.

By implication, subscripting an empty string is undefined.
The value in the subscript is referred to as “a subscript” or “an index.” The index we supply can be any expression that yields an integral value. However, if our index has a signed type, its value will be converted to the unsigned type that string::size_type represents (§ 2.1.2, p. 36).

The following example uses the subscript operator to print the first character in a string:

  1. if (!s.empty()) // make sure there’s a character to print   
  2. cout << s[0] << endl; // print the first character in s 

Before accessing the character, we check that s is not empty. Any time we use a subscript, we must ensure that there is a value at the given location. If s is empty, then s[0] is undefined.

So long as the string is not const (§ 2.4, p. 59), we can assign a new value to the character that the subscript operator returns. For example, we can capitalize the first letter as follows:

  1. string s("some string");   
  2. if (!s.empty()) // make sure there’s a character in s[0]   
  3. s[0] = toupper(s[0]); // assign a new value to the first character in s  

The output of this program is

Some string

Using a Subscript for Iteration

As a another example, we’ll change the first word in s to all uppercase:

  1. // process characters in s until we run out of characters or we hit a whitespace   
  2. for (decltype(s.size()) index = 0;   
  3. index != s.size() && !isspace(s[index]); ++index)   
  4. s[index] = toupper(s[index]); // capitalize the current character  

This program generates

SOME string

Our for loop (§ 1.4.2, p. 13) uses index to subscript s. We use decltype to give index the appropriate type. We initialize index to 0 so that the first iteration will start on the first character in s. On each iteration we increment index to look at the next character in s. In the body of the loop we capitalize the current letter.

The new part in this loop is the condition in the for. That condition uses the logical AND operator (the && operator). This operator yields true if both operands are true and false otherwise. The important part about this operator is that we are guaranteed that it eva luates its right-hand operand only if the left-hand operand is true. In this case, we are guaranteed that we will not subscript s unless we know that index is in range. That is, s[index] is executed only if index is not equal to s.size(). Because index is never incremented beyond the value of s.size(), we know that index will always be less than s.size().

CAUTION: SUBSCRIPTS ARE UNCHECKED

When we use a subscript, we must ensure that the subscript is in range. That is, the subscript must be >= 0 and < the size() of the string. One way to simplify code that uses subscripts is always to use a variable of type string::size_type as the subscript. Because that type is unsigned, we ensure that the subscript cannot be less than zero. When we use a size_type value as the subscript, we need to check only that our subscript is less than value returned by size().

The library is not required to check the value of an subscript. The result of using an out-of-range subscript is undefined.

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

评论

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

·用 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)