博客
关于我
STL常用运算符重载
阅读量:302 次
发布时间:2019-03-03

本文共 1020 字,大约阅读时间需要 3 分钟。

#include 
#include
#include
#include
using namespace std;class Person {public: Person(string name, string no, int age) { this->sname = name; this->sno = no; this->sage = age; } bool operator<(const Person& p) const { if (this->sno < p.sno) return true; return false; } friend ostream& operator<<(ostream& os, const Person& p1) { cout << "姓名:" << p1.sname << " 学号:" << p1.sno << " 年龄:" << p1.sage << endl; return os; }public: string sname; string sno; int sage;};int main() { Person p3("王麻子", "18170025", 18); Person p1("张三", "18170021", 19); Person p2("李四", "18170022", 20); set
st; st.insert(p2); st.insert(p1); st.insert(p3); set
::iterator it; for (it = st.begin(); it != st.end(); it++) cout << *it; system("pause"); return 0;}

以上代码定义了一个Person类,并为其定义了一个基于学号的排序运算符和一个用于输出的友元运算符。代码还展示了如何在main函数中创建Person对象并使用这些运算符进行操作。

转载地址:http://vksm.baihongyu.com/

你可能感兴趣的文章
PXE+HTTP+TFTP+Kickstart实现无人值守部署centos6.10
查看>>
pxe实现linux的自动安装
查看>>
py 的 第 19 天
查看>>
Py-Bitcoin 项目使用教程
查看>>
Pytorch 图像增强 实现翻转裁剪色调等 附代码(全)
查看>>
pyautogui模拟鼠标拖动选中文字的基本知识(附Demo)
查看>>
pyautogui的基本介绍和使用
查看>>
PyCharm - 社区版是否能够突出显示 css/javascript?
查看>>
PyCharm 2018.3.5 RC 发布,原生 SSH 支持
查看>>
PyCharm 3.1 在索引期间永远挂起并且无法使用
查看>>
Pycharm Pro 2018.2 汉化专业激活破解
查看>>
PyCharm vs VSCode,是时候改变你的 IDE 了!
查看>>
PyCharm 中,“新建”(New)和“新建项目”(New Project)-ChatGPT4o作答
查看>>
PyCharm 代码编辑与调试运行详解
查看>>
Pycharm 出现 instantitaing tests 以及test session starts 的解决方法
查看>>
pytorch 固定随机种子
查看>>
Pycharm 对容器中的 Python 程序断点远程调试
查看>>
Pycharm 常用快捷键大全【快查字典版】
查看>>
Pycharm 常用快捷键大全,全网最全!
查看>>
PyCharm 常用的技巧完全指南
查看>>