STL——increment/decrement/dereference操作符
发布日期:2021-06-24 18:06:45 浏览次数:4 分类:技术文章

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

increment/dereference操作符在迭代器的实现上占有非常重要的地位,因为任何一个迭代器都必须实现出前进(increment,operator++)和取值(dereference,operator*)功能,前者还分为前置式(prefix)和后置式(Postfix)两种。有写迭代器具备双向移动功能,那么就必须再提供decrement操作符(也分前置式和后置式),下面是一个例子:

#include
using namespace std;class INT{ friend ostream& operator<<(ostream& os,const INT& i);public: INT(int i):m_i(i){} INT& operator++() { ++(this->m_i); return *this; } const INT operator++(int) { INT temp=*this; ++(*this); return temp; } INT& operator--() { --(this->m_i); return *this; } const INT operator--(int) { INT temp=*this; --(*this); return temp; } int& operator*() const { return (int&)m_i; }private: int m_i;};ostream& operator<<(ostream& os,const INT &i){ os<<'['<
<<']'; return os;}int main(){ INT I(5); cout<

  运行结果:

转载地址:https://blog.csdn.net/weixin_34248258/article/details/85666429 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:exit和wait一起可以彻底清除子进程的资源
下一篇:敏捷开发流程总结

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月12日 06时18分44秒