设为首页 加入收藏

TOP

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

A string is a variable-length sequence of characters. To use the string type, we must include the string header. Because it is part of the library, string is defined in the std namespace. Our examples assume the following code:

  1. #include <string>   
  2. using std::string;  

This section describes the most common string operations; § 9.5 (p. 360) will cover additional operations.

In addition to specifying the operations that the library types provide, the standard also imposes efficiency requirements on implementors. As a result, library types are efficient enough for general use.

3.2.1 Defining and Initializing strings

Each class defines how objects of its type can be initialized. A class may define many different ways to initialize objects of its type. Each way must be distinguished from the others either by the number of initializers that we supply, or by the types of those initializers. Table 3.1 lists the most common ways to initialize strings. Some examples:

  1. string s1; // default initialization; s1 is the empty string   
  2. string s2 = s1; // s2 is a copy of s1   
  3. string s3 = "hiya"// s3 is a copy of the string literal   
  4. string s4(10, ’c’); // s4 is cccccccccc  

We can default initialize a string (§ 2.2.1, p. 44),which creates an empty string; that is, a stringwith no characters. henwe supply a string literal (§ 2.1.3, p. 39), the characters from that literal—up to but not including the null character at the end of the literal—are copied into the newly created string. When we supply a count and a character, the string contains thatmany copies of the given character.

Direct and Copy Forms of Initialization

In § 2.2.1 (p. 43)we saw that C++(www.cppentry.com) has several different forms of initialization. Using strings, we can start to understand how these forms differ from one another. When we initialize a variable using =,we are asking the compiler to copy initialize the object by copying the initializer on the right-hand side into the object being created. Otherwise, when we omit the =, we use direct initialization.

When we have a single initializer, we can use either the direct or copy form of initialization. When we initialize a variable from more than one value, such as in the initialization of s4 above, we must use the direct form of initialization:

  1. string s5 = "hiya"// copy initialization   
  2. string s6("hiya"); // direct initialization   
  3. string s7(10, ’c’); // direct initialization; s7 is cccccccccc  

When we want to use several values, we can indirectly use the copy form of initialization by explicitly creating a (temporary) object to copy:

  1. string s8 = string(10, ’c’); // copy initialization; s8 is cccccccccc  

The initializer of s8—string(10, ’c’)—creates a string of the given size and character value and then copies that value into s8. It is as if we hadwritten

  1. string temp(10, ’c’); // temp is cccccccccc   
  2. string s8 = temp; // copy temp into s8  

Although the code used to initialize s8 is legal, it is less readable and offers no compensating advantage over the way we initialized s7.

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

评论

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

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