1 Chapter objectives To provide a standardized library for character string handling Overloaded...
-
Upload
sophia-marsh -
Category
Documents
-
view
239 -
download
0
Transcript of 1 Chapter objectives To provide a standardized library for character string handling Overloaded...
1
Chapter objectives
• To provide a standardized library for character string handling
• Overloaded operators and member functions as a facility
補充 : C++ string Class
2
String Class Constructors
3
String Class Overloaded Operatorsa
(1/2)
4
String Class Overloaded Operatorsa
(2/2)
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;}
6
7
String Editing Functions
常用的編輯字串指令 應改成 8 才對
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;}
9
10
String Searching Functions
常用的字串搜尋指令
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;}
12
13
Miscellaneous String Functions (1/2)
14
Miscellaneous String Functions (2/2)
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;}
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;}
練習• 撰寫程式從鍵盤依序讀入若干個名字,迨使用者輸入
空字串則停止讀入,隨即依姓名進行排序並顯示結果
17