设为首页 加入收藏

TOP

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

3.2.2 Operations on strings

Along with defining how objects are created and initialized, a class also defines the operations that objects of the class type can perform. A class can define operations that are called by name, such as the isbn function of our Sales_item class (§ 1.5.2, p. 23). A class also can define what various operator symbols, such as << or +, mean when applied to objects of the class’ type. Table 3.2 (overleaf) lists the most common string operations.

Reading andWriting strings

As we saw in Chapter 1, we use the iostream library to read and write values of built-in types such as int, double, and so on. We use the same IO operators to read and write strings:

  1. // Note: #include and using declarations must be added to compile this code   
  2. int main()   
  3. {   
  4. string s; // empty string   
  5. cin >> s; // read a whitespace-separated string into s   
  6. cout << s << endl; // write s to the output   
  7. return 0;   

This program begins by defining an empty string named s. The next line reads the standard input, storing what is read in s. The string input operator reads and discards any leading whitespace (e.g., spaces, newlines, tabs). It then reads characters until the next whitespace character is encountered.

So, if the input to this program is Hello World! (note leading and trailing spaces), then the output will be Hello with no extra spaces.

Like the input and output operations on the built-in types, the string operators return their left-hand operand as their result. Thus, we can chain together multiple reads or writes:

  1. string s1, s2;   
  2. cin >> s1 >> s2; // read first input into s1, second into s2   
  3. cout << s1 << s2 << endl; // write both strings  

If we give this version of the program the same input, Hello World! , our output would be “HelloWorld!”

Reading an Unknown Number of strings

In § 1.4.3 (p. 14) we wrote a programthat read an unknown number of int values. We can write a similar program that reads strings instead:

  1. int main()   
  2. {   
  3. string word;   
  4. while (cin >> word) // read until end-of-file   
  5. cout << word << endl; // write each word followed by a new line   
  6. return 0;   
  7. }  

In this program, we read into a string, not an int. Otherwise, the while condition executes similarly to the one in our previous program. The condition tests the stream after the read completes. If the stream is valid—it hasn’t hit end-of-file or encountered an invalid input—then the body of the while is executed. The body prints the value we read on the standard output. Once we hit end-of-file (or invalid input), we fall out of the while.

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

评论

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

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