下载此文档

软件设计(II)教学课件:Chapter15 String.ppt


文档分类:IT计算机 | 页数:约13页 举报非法文档有奖
1/13
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/13 下载此文档
文档列表 文档介绍
Chapter 15 String
*
*
The C++ “string” Class
A C++ library class defined in <string>
With more features than C-string functions
*
Constructing a String
A string object with empty string:
string newString;
A string object from a string literal or an char array:

string newString(stringLiteral);
*
Appending a String
string s1("Welcome");
(" to C++"); // appends " to C++" to s1
cout << s1 << endl; //Welcome to C++
string s2("Welcome");
(" to C and C++", 0, 5);
cout << s2 << endl; //Welcome C an
string s3("Welcome");
(" to C and C++", 5);
cout << s3 << endl; //Welcome to C
string s4("Welcome");
(4, 'G');
cout << s4 << endl; //WelcomeGGGG
*
Assigning a String
You can use several overloaded functions to assign new contents to a string. For example, see the following code:
string s1("Welcome");
("Dallas"); // assigns "Dallas" to s1
cout << s1 << endl; // s1 now becomes Dallas
string s2("Welcome");
("Dallas, Texas", 0, 5); // assigns "Dalla" to s2
cout << s2 << endl; // s2 now becomes Dalla
string s3("Welcome");
("Dallas, Texas", 5); // assigns "Dalla" to s3
cout << s3 << endl; // s3 now becomes Dalla
string s4("Welcome");
(4, 'G'); // assigns "GGGG" to s4
cout << s4 << endl; // s4 now becomes GGGG
*
Functions at, clear, erase, and empty
You can use the at(index) function to retrieve a character at a specified index, clear() to clear the string, erase(index, n) to delete part of the string, and empty() to test if a string is empty. For example, see the following code:
string s1("Welcome");
cout << (3) << endl; // (3) returns c
cout << (2, 3) << endl; // s1 is now Weme
(); // s1 is now empty
cout << () << endl; // returns 1 (means true)
*
Comparing Strings
Often, in a program, you need to compare the contents of two strings. You can use the compare function. This function works in the same way as the C-string s

软件设计(II)教学课件:Chapter15 String 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数13
  • 收藏数0 收藏
  • 顶次数0
  • 上传人窝窝爱蛋蛋
  • 文件大小528 KB
  • 时间2021-11-30