1 chapter objectives to provide a standardized library for character string handling overloaded...

17
1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補補 : C++ string Class

Upload: sophia-marsh

Post on 13-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

1

Chapter objectives

• To provide a standardized library for character string handling

• Overloaded operators and member functions as a facility

補充 : C++ string Class

Page 2: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

2

String Class Constructors

Page 3: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

3

String Class Overloaded Operatorsa

(1/2)

Page 4: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

4

String Class Overloaded Operatorsa

(2/2)

Page 5: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

5

/* Program G-1 String example program with several string constructors and overloaded operators */#include <iostream> //C++ header style since using C++ string class#include <string>

using namespace std;

int main(){

string S1("What a great day."), S2, S3;char MyOldString[25] = "Howdy Partner";

string S4(MyOldString);S2 = "Do you love C++?";//assignmentS3 = S1 + S2;

//Write the initial string valuescout << "\n S1 = " << S1;cout << "\n S2 = " << S2;cout << "\n S3 = " << S3;cout << "\n S4 = " << S4;cout << "\n MyOldString = " << MyOldString << endl;

if(S1 > S2)cout << "\n S1 is greater than S2";else cout << "\n S1 is not greater than or the same as S2";

S1 = "\nAll done with strings.";cout << S1 << endl;

return 0;}

Page 6: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

6

Page 7: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

7

String Editing Functions

常用的編輯字串指令 應改成 8 才對

Page 8: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

8

// Program G-2 String Class Example Program using Editing Functions#include <iostream> //C++ header style since using C++ string class#include <string>

using namespace std;

int main(){

string S1("It is a rainy day.");string S2("very ");string S3("sunny ");

//Write the initial string valuescout << "\n S1 = " << S1;cout << "\n S2 = " << S2;cout << "\n S3 = " << S3;// Insert "very " into S1S1.insert(8,S2);cout << "\n S1 = " << S1;// Replace "rainy" with "sunny" in S1S1.replace(13, 6, S3);cout << "\n S1 = " << S1;// Erase "very" from S1S1.erase(8, 5);cout << "\n S1 = " << S1;

S1 = "\nAll done with strings.";cout << S1 << endl;

return 0;}

Page 9: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

9

Page 10: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

10

String Searching Functions

常用的字串搜尋指令

Page 11: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

11

// Program G-3 String Class Example Program using Search Functions#include <iostream> //C++ header style since using C++ string class#include <string>#include <valarray>

using namespace std;

int main(){

string S1("One potato, two potato, three potato, four.");string S2("potato");int First_pos, Last_pos;

//Write the initial string valuescout << "\n S1 = " << S1;cout << "\n S2 = " << S2;// Search S1 for "potato" from start of the stringFirst_pos = S1.find(S2,0);cout << "\n The first \"potato\" is at " << First_pos;

// string::npos is defined as the length of the stringLast_pos = S1.rfind(S2,string::npos); cout << "\n The last \"potato\" is at " << Last_pos;S1 = "\nAll done with strings.";cout << S1 << endl;

return 0;}

Page 12: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

12

Page 13: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

13

Miscellaneous String Functions (1/2)

Page 14: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

14

Miscellaneous String Functions (2/2)

Page 15: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

15

Conversion between Char Arrays and Strings

// Program G-4 Convert Between Strings and Character String Arrays#include <iostream> //C++ header style since using C++ string class#include <string>using namespace std;

int main(){

string S1("I like the sea and C, you see.");string S2;char MyArray[50] = "Is C++ really a B-?"; const char *MyOtherArray;

cout << "\n The original string and array" << endl;cout << S1 << endl;cout << MyArray << endl;

//First we place MyArray contents into S2 using the assignS2.assign(MyArray);

//Next, we'll stuff the contents of S1 into MyOtherArrayMyOtherArray = S1.c_str(); //convert a string to a const char*

cout << "\n The converted array and string" << endl;cout << MyOtherArray << endl;cout << S2 << endl;

return 0;}

Page 16: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

16

Reading into a String (e.g., from the Keyboard)

• To read text with whitespace characters– Must use get or getline and then assign the

contents into a string// Program G-5 Reading into a String #include <iostream> #include <string>using namespace std;

int main(){

string S1;char MyArray[50];

cout << "\n Enter your favorite string saying " << endl;cin.getline(MyArray,50);

//Assign the array contents into S1S1.assign(MyArray);cout << "\n Here is your saying twice! "<< endl;cout << MyArray << endl;cout << S1 << endl;

return 0;}

Page 17: 1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string

練習• 撰寫程式從鍵盤依序讀入若干個名字,迨使用者輸入

空字串則停止讀入,隨即依姓名進行排序並顯示結果

17