Download presentation
Presentation is loading. Please wait.
Published bySophia Marsh Modified over 9 years ago
1
1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string Class
2
2 String Class Constructors
3
3 String Class Overloaded Operators a (1/2)
4
4 String Class Overloaded Operators a (2/2)
5
5 /* Program G-1 String example program with several string constructors and overloaded operators */ #include //C++ header style since using C++ string class #include 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++?";//assignment S3 = S1 + S2; //Write the initial string values cout << "\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; }
6
6
7
7 String Editing Functions 常用的編輯字串指令 應改成 8 才對
8
8 // Program G-2 String Class Example Program using Editing Functions #include //C++ header style since using C++ string class #include using namespace std; int main() { string S1("It is a rainy day."); string S2("very "); string S3("sunny "); //Write the initial string values cout << "\n S1 = " << S1; cout << "\n S2 = " << S2; cout << "\n S3 = " << S3; // Insert "very " into S1 S1.insert(8,S2); cout << "\n S1 = " << S1; // Replace "rainy" with "sunny" in S1 S1.replace(13, 6, S3); cout << "\n S1 = " << S1; // Erase "very" from S1 S1.erase(8, 5); cout << "\n S1 = " << S1; S1 = "\nAll done with strings."; cout << S1 << endl; return 0; }
9
9
10
10 String Searching Functions 常用的字串搜尋指令
11
11 // Program G-3 String Class Example Program using Search Functions #include //C++ header style since using C++ string class #include 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 values cout << "\n S1 = " << S1; cout << "\n S2 = " << S2; // Search S1 for "potato" from start of the string First_pos = S1.find(S2,0); cout << "\n The first \"potato\" is at " << First_pos; // string::npos is defined as the length of the string Last_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; }
12
12
13
13 Miscellaneous String Functions (1/2)
14
14 Miscellaneous String Functions (2/2)
15
15 Conversion between Char Arrays and Strings // Program G-4 Convert Between Strings and Character String Arrays #include //C++ header style since using C++ string class #include 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 assign S2.assign(MyArray); //Next, we'll stuff the contents of S1 into MyOtherArray MyOtherArray = 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; }
16
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 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 S1 S1.assign(MyArray); cout << "\n Here is your saying twice! "<< endl; cout << MyArray << endl; cout << S1 << endl; return 0; }
17
練習 撰寫程式從鍵盤依序讀入若干個名字,迨使用者輸入 空字串則停止讀入,隨即依姓名進行排序並顯示結果 17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.