8.2 The C++ string Class (continued ) Comparing string Objects : use the <, >, <=,>=, ==, and != relational operators. For example:setl = "ABC":stringset2 = "XYZ"':stringif (setl < set2)cout<<"setl is less than set2.ln";
6 8.2 The C++ string Class (continued ) • Comparing string Objects : use the <, >, <=, >=, ==, and != relational operators. • For example: string set1 = "ABC"; string set2 = "XYZ"; if ( set1 < set2 ) cout << "set1 is less than set2.\n";
8.2 The C++ string Class (continued Other Waysto Declare string Objects:1.stringgaddress;Declares an empty string object.2. string name("Zhang San);3.string personl(person2);person2 : a string object or character array
7 8.2 The C++ string Class (continued ) • Other Ways to Declare string Objects: 1. string address; Declares an empty string object. 2. string name("Zhang San"); 3. string personl(person2); person2 : a string object or character array
8.2 The C++ string Class (continued4. stringsetl(set2, 5);采用set2的前5个字符初始化setl5. stringlineFull(z, 10);initialized with 10z'characters.6. stringfirstName(fullName,0,7);用fullName从位置0开始的7个字符,初始化firstName
8 8.2 The C++ string Class (continued ) 4. string setl(set2, 5); 采用set2的前5个字符初始化set1. 5. string lineFull( 'z', 10); initialized with 10 ' z' characters. 6. string firstName(fullName,0,7); 用fullName 从位置0开始的7个字符,初始 化firstName
8.2 The C++ string Class (continued The string class supports several operators:[I Implements array-subscript notation, as inname [x].Relationaloperators>,V,V",>,m,!例如:
9 8.2 The C++ string Class (continued ) • The string class supports several operators: >>、<<、= 、+=、+ [ ] Implements array-subscript notation, as in name [x]. Relational operators <,>,>=,<=, == ,!= 例如:
#include<iostream>#include<string>std;usingnamespacevoid main(void)stringstrl-"ABC",str2=-"DEF",str3;str3=strl+str2;for (int x = 0; x< str3.size(); x++)cout << str3[x];cout << endl;if (strl< str2)cout<<"strlis less than str2n";elsecout<<"strlis not less than str2ln
#include <iostream> #include <string> using namespace std; void main(void) { string strl="ABC", str2="DEF", str3; str3 = strl + str2; for (int x = 0; x < str3.size( ); x++) cout << str3[x]; cout << endl; if (strl < str2) cout << "strl is less than str2\n"; else cout << "strl is not less than str2\n"; }