来源于c++大学教程 测试最有代表性的c++标准的string类 #include iostream#include stringusing namespace std;int main(){ string s1("happy"); string s2(" birthday"); string s3; cout s1 endl s2 endl s3 endl; cout "s1 == s2:" (s1
来源于<<c++大学教程>>
测试最有代表性的c++标准的string类
#include <iostream> #include <string> using namespace std; int main() { string s1("happy"); string s2(" birthday"); string s3; cout << s1 << endl << s2 << endl << s3 << endl; cout << "s1 == s2:" << (s1 == s2 ? "true" : "false") << endl << "s1 != s2:" << (s1 != s2 ? "true" : "false") << endl << "s1 < s2:" << (s1 < s2 ? "true" : "false") << endl << "s1 >= s2:" << (s1 >= s2 ? "true" : "false") << endl << "s1 <= s2:" << (s1 <= s2 ? "true" : "false") << endl; cout << "test s3.empty():" << endl; if (s3.empty()) { cout << "s3 is empty:assigning s1 to s3;" << endl; s3 = s1; cout << "s3:" << s3 << endl; } s1 += s2; cout << "s1+=s2:" << endl; cout << "s1:" << s1 << endl; s1 += " to you"; cout << "s1+= \"to you \"" << endl; cout << "s1:" << s1 << endl; cout << "substring is 0-14 is :" << s1.substr(0, 14) << endl; string s4(s1); cout << "test copy string s4(s1) : s4 is " << s4 << endl; s4 = s4; cout << "test s4=s4 s4:" << s4 << endl; s1[0] = 'H'; s1[6] = 'B'; cout << "after s1[0] = 'H'; s1[6] = 'B'; s1 is " << s1 << endl; try { cout << "attempt to assign 'd' to s1.at(30) yields:" << endl; s1.at(30) = 'd';//error } catch (out_of_range &ex) { cout << "an exception occurred:" << ex.what() << endl; } return 0; }
重载二元操作符-两种方式
作为成员函数的二元重载运算符
bool operator<(const String &)const;
非成员函数的二元重载运算符
bool operator<(const String&,const String &);