设为首页 加入收藏

TOP

8.11.2 连接字符串(1)
2013-10-07 16:09:53 来源: 作者: 【 】 浏览:67
Tags:8.11.2 连接 字符串

8.11.2  连接字符串(1)

字符串最常见的运算可能是连接两个字符串形成一个新的字符串了。可以用+运算符连接两个字符串对象或者一个字符串对象和一个字符串字面值。下面是一些示例:
 

  1. string sentence1("This sentence is false.");  
  2. string sentence2("Therefore the sentence above must be true!");  
  3. string combined;                                                                                // Create an empty string  
  4. sentence1sentence1 = sentence1 + "\n";                                   // Append string containing newline  
  5. combined = sentence1 + sentence2;                   // Join two strings  
  6. cout << combined << endl;                                               // Output the result  

执行这些语句将得到下面的输出:

  1. This sentence is false.  
  2. Therefore the sentence above must be true!  

前3个语句创建字符串对象。下一个语句将字符串字面值"\n"附加到sentence1后面,并将结果存储在sentence1中。再下一个语句连接sentence1和sentence2,并将结果存储在combined中。最后一个语句输出字符串combined。

字符串可以用+运算符连接,是因为string类实现了operator+()。这意味着操作数之一必须为string对象,因此不能用+运算符连接两个字符串字面值。记住,每次用+运算符连接两个字符串时,都是在创建一个新的string对象,这会带来一定的开销。下一节将介绍如何修改和扩展现有的string对象,在有些情况下这可能是一种更有效的方法,因为它不涉及创建新对象。

也可以用+运算符将一个字符连接到一个字符串对象上,因此前面代码片段中的第4个语句可以写成:
 

  1. sentence1sentence1 = sentence1 + '\n';                               // Append newline character to string 

string类也可以实现operator+=(),因此右操作数可以是一个字符串字面值、一个字符串对象或者一个字符。前面的语句可以写成:

  1. sentence1 += '\n'; 

或者写成:

  1. sentence1 += "\n"; 

使用+=运算符与使用+运算符有一个区别。如前所述,+运算符创建一个包含合并后的字符串的新字符串对象。+=运算符将作为右操作数的字符串或字符附加到作为左操作数的string对象后面,因此直接修改string 对象,而不创建新的对象。

下面用一个示例来练习上面描述的概念。

试一试:创建与连接字符串

这一简单示例通过键盘输入姓名和年龄,然后列出输入的内容。代码如下:

  1. // Ex8_13.cpp  
  2. // Creating and joining string objects  
  3. #include <iostream> 
  4. #include <string> 
  5. using std::cin;  
  6. using std::cout;  
  7. using std::endl;  
  8. using std::string;  
  9. using std::getline;  
  10. // List names and ages  
  11. void listnames(string names[], string ages[], size_t count)  
  12. {  
  13. cout << endl << "The names you entered are: " << endl;  
  14. for(size_t i = 0 ; i < count && !names[i].empty() ; ++i)  
  15. cout << names[i] + " aged " + ages[i] + '.' << endl;  
  16. }  
  17. int main()  
  18. {  
  19. const size_t count = 100;  
  20. string names[count];  
  21. string ages[count];  
  22. string firstname;  
  23. string secondname;  
  24. for(size_t i = 0 ; i < count ; i++)  
  25. {  
  26. cout << endl << "Enter a first name or press Enter to end: ";  
  27. getline(cin, firstname, '\n');  
  28. if(firstname.empty())  
  29. {  
  30. listnames(names, ages, i);  
  31. cout << "Done!!" << endl;  
  32. return 0;  
  33. }  
  34. cout << "Enter a second name: ";  
  35. getline(cin, secondname, '\n');  
  36. names[i] = firstname + ' ' + secondname;  
  37. cout << "Enter " + firstname + "'s age: ";  
  38. getline(cin, ages[i], '\n');  
  39. }  
  40. cout << "No space for more names." << endl;  
  41. listnames(names, ages, count);  
  42. return 0;  
  43. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.11.1 创建字符串对象 下一篇8.11.2 连接字符串(2)

评论

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

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)