如何将基础运算符重载改写成长尾?

2026-04-16 23:522阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计491个文字,预计阅读时间需要2分钟。

如何将基础运算符重载改写成长尾?

从C++大学教材和最具代表性的C++标准库中的string类出发,深入探讨string类的特性和应用。

来源于<<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 &);

本文共计491个文字,预计阅读时间需要2分钟。

如何将基础运算符重载改写成长尾?

从C++大学教材和最具代表性的C++标准库中的string类出发,深入探讨string类的特性和应用。

来源于<<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 &);