8.2 The C++ string Class (continued . Some memberfunctions:1. s1.append(str);2. s1.assign(str);3. s1.at(x);4. sl.clearO;// like strcmp(sl, str)5. s1.compare(str);
11 8.2 The C++ string Class (continued ) • Some member functions: 1. s1.append(str); 2. s1.assign(str); 3. s1.at(x); 4. s1.clear( ); 5. s1.compare(str); // like strcmp(s1, str)
8.2 The C++ string Class (continued 6. sl.dataO;// returns a character array.7. sl.empty();8. s1.length();9. sl.size();10. s1.substr(x, n);11. s1.swap(str);//swapsthe contents of sl withstr
12 8.2 The C++ string Class (continued ) 6. s1.data( ); // returns a character array. 7. s1.empty( ); 8. s1.length( ); 9. s1.size( ); 10. s1.substr(x, n); 11. s1.swap(str); //swaps the contents of s1 with str
ACaseStudyassume a string object contains a valuesuchas 1084567.89=¥1,084.567.89·例如:Program1-47
13 A Case Study • assume a string object contains a value such as 1084567.89 ¥1,084,567.89 • 例如:Program 1-47
#include<iostream>#include<string>std;usingnamespace&);voidRMBFormat(stringvoid main(void)string input;cout <<"Enter a RMB amount (nnnnn.nn):cin >> input;RMBFormat(input); // function callcout << "Here is the amount formatted:cout <<input << endl;
#include <iostream> #include <string> using namespace std; void RMBFormat( string & ); void main(void) { string input; cout << "Enter a RMB amount (nnnnn.nn): "; cin >> input; RMBFormat(input); // function call cout << "Here is the amount formatted: "; cout << input << endl; }
/*******************************************// function formats the number// as a RMB amount with commas and a Y symbol.*******************************************voidRMBFormat(string& currency)intdp;dp = currency.find(.); // Find decimal pointif (dp >3) (// Insert commasfor (int x=dp-3; x>0; x-=3)currency.insert(x,",");currency.insert(O,“?");// Insert RMB sign
//******************************************* // function formats the number // as a RMB amount with commas and a ¥symbol. //******************************************* void RMBFormat(string & currency) { int dp; dp = currency.find('.'); // Find decimal point if (dp > 3) { // Insert commas for (int x = dp - 3; x > 0; x -= 3) currency.insert(x, ","); } currency.insert(0, “¥"); // Insert RMB sign }