设为首页 加入收藏

TOP

3.2 Library string Type (3)
2013-10-07 16:19:12 来源: 作者: 【 】 浏览:58
Tags:3.2 Library string Type

Using getline to Read an Entire Line

Sometimes we do not want to ignore the whitespace in our input. In such cases, we can use the getline function instead of the >> operator. The getline function takes an input stream and a string. This function reads the given stream up to and including the first newline and stores what it read—not including the newline—in its string argument. After getline sees a newline, even if it is the first character in the input, it stops reading and returns. If the first character in the input is a newline, then the resulting string is the empty string.

Like the input operator, getline returns its istream argument. As a result, we can use getline as a condition just as we can use the input operator as a condition (§ 1.4.3, p. 14). For example, we can rewrite the previous program that wrote one word per line to write a line at a time instead:

  1. int main()   
  2. {   
  3. string line;   
  4. // read input a line at a time until end-of-file   
  5. while (getline(cin, line))   
  6. cout << line << endl;   
  7. return 0;   
  8. }  

Because line does not contain a newline, we must write our own. As usual, we
use endl to end the current line and flush the buffer.

The newline that causes getline to return is discarded; the newline is not stored in the string.

The string empty and size Operations

The empty function does what one would expect: It returns a bool (§ 2.1, p. 32) indicating whether the string is empty. Like the isbn member of Sales_item (§ 1.5.2, p. 23), empty is amember function of string. To call this function, we use the dot operator to specify the object on which we want to run the empty function.

We can revise the previous program to only print lines that are not empty:

  1. // read input a line at a time and discard blank lines   
  2. while (getline(cin, line))   
  3. if (!line.empty())   
  4. cout << line << endl;  

The condition uses the logical NOT operator (the !operator). This operator returns the inverse of the bool value of its operand. In this case, the condition is true if str is not empty.
The size member returns the length of a string (i.e., the number of characters
in it). We can use size to print only lines longer than 80 characters:

  1. string line;   
  2. // read input a line at a time and print lines that are longer than 80 characters   
  3. while (getline(cin, line))   
  4. if (line.size() > 80)   
  5. cout << line << endl;  

The string::size_type Type

It might be logical to expect that size returns an int or, thinking back to § 2.1.1 (p. 34), an unsigned. Instead, size returns a string::size_type value. This type requires a bit of explanation.

The string class—and most other library types—defines several companion types. These companion types make it possible to use the library types in a machine-independent manner. The type size_type is one of these companion types. To use the size_type defined by string, we use the scope operator to say that the name size_type is defined in the string class.

Although we don’t know the precise type of string::size_type, we do know that it is an unsigned type (§ 2.1.1, p. 32) big enough to hold the size of any string. Any variable used to store the result from the string size operation should be of type string::size_type.

Admittedly, it can be tedious to type string::size_type. Under the new standard, we can ask the compiler to provide the appropriate type by using auto or decltype (§ 2.5.2, p. 68):

  1. auto len = line.size(); // len has type string::size_type  

Because size returns an unsigned type, it is essential to remember that expressions that mix signed and unsigned data can have surprising results (§ 2.1.2, p. 36). For example, if n is an int that holds a negative value, then s.size() < n will almost surely eva luate as true. It yields true because the negative value in n will convert to a large unsigned value.

You can avoid problems due to conversion between unsigned and int by not using ints in expressions that use size().

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

评论

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

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