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

#include<iostream>
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<<'['<<i.m_i<<']';
return os;
} int main()
{
INT I(5);
cout<<I++;
cout<<++I;
cout<<I--;
cout<<--I;
cout<<*I;
}

  运行结果:

STL——increment/decrement/dereference操作符的更多相关文章

  1. increment/decrement/dereference操作符

    标题以上分别对于++/--/* #include <iostream> #include <cstddef> using namespace std; class INT { ...

  2. increment/decrement/dereference

    #include <vector> #include <deque> #include <algorithm> #include <iostream> ...

  3. ITEM M6 自增(INCREMENT)、自减(DECREMENT)操作符前缀形式与后缀形式的区别

    前缀自增 UPInt & UPint::operator++() { *this+=1; return *this; } 后缀自增 const UPInt & UPint::opera ...

  4. 【M6】区别increment/decrement操作符的前置(prefix)和后置(postfix)形式

    1.考虑++(--的情况是一样的),前置是累加然后取出,后置是取出然后累加. 2.重载方法根据形参表的不同区分,问题来了,前置和后置形式都没有形参,因此没法区分.怎么办? 对于后置增加一个形参int, ...

  5. 侯捷STL学习(四)--OOP-GP/操作符重载-泛化特化

    C++标准库第二讲 体系结构与内核分析 第1-7节为第一讲 读源代码前的准备 第八节:源代码分布 C++基本语法 模板的使用 数据结构和算法 本课程主要使用:Gnu C 2.9.1与Gun C 4.9 ...

  6. [atAGC049E]Increment Decrement

    由于每一个操作的逆操作都存在,可以看作将$a_{i}$全部变为0的代价 先考虑第一个问题,即对于确定的$a_{i}$如何处理 如果仅能用第2种操作,定义点$i$的代价为以$i$为左端点或以$i-1$为 ...

  7. 《STL源码剖析》学习半生记:第一章小结与反思

    不学STL,无以立.--陈轶阳 从1.1节到1.8节大部分都是从各方面介绍STL, 包括历史之类的(大致上是这样,因为实在看不下去我就直接略到了1.9节(其实还有一点1.8.3的内容)). 第一章里比 ...

  8. noobSTL-1-配置器-1

    noobSTL-1-配置器-1 1.要点分析 1.1 可能让你困惑的C++语法 组态 即配置. 临时对象 一种无名对象.有时候会刻意地制造临时对象. 静态常量整数成员在class内部直接初始化 con ...

  9. noobSTL-1-配置器-0

    noobSTL-1-配置器-0 0.前言 STL的配置器(allocator),也叫内存分配器,负责空间配置与管理,简单地说,就是负责管理内存的. 从实现的角度来看,配置器是一个实现了动态空间配置.空 ...

随机推荐

  1. (转载)css垂直水平居中的整理

    方法一 .demo1 { width:180px; height:180px; line-height:180px; *font-size:160px; border:1px solid #ddd; ...

  2. 封装cookie组件

    var Cookie = { // 读取 get: function(name){ var cookieStr = "; "+document.cookie+"; &qu ...

  3. input file文件上传样式

    <style>    .file-group {        position: relative;        width: 200px;        height: 80px;  ...

  4. [jQuery编程挑战]005 使用最短的代码生成元素的闪烁效果

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  5. 去除VS2010中中文注释下的红色波浪线

    1,中文注释以分号结尾 2,Assist X 菜单栏->Assist X Option->Underline 选择“min”.

  6. 使用C语言获取当前系统的时间

    要想使用C语言来获取当前系统的时间,办法如下: 需要提前准备的工作: #include <stdio.h> #include <time.h> #include <std ...

  7. bzoj2597: [Wc2007]剪刀石头布

    Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道 ...

  8. kafka笔记-Kafka在zookeeper中的存储结构【转】

    参考链接:apache kafka系列之在zookeeper中存储结构  http://blog.csdn.net/lizhitao/article/details/23744675 1.topic注 ...

  9. go bufio

    1 bufio 读结构 type Reader struct {    buf          []byte     // 缓冲区    rd           io.Reader // read ...

  10. Rabbit hunt

    poj2606:http://poj.org/problem?id=2606 给你n个点,求在一条直线上的点最多有几个.题解:直接暴力,但是要注意,横坐标相等的情况,这是不能求斜力,只能特殊处理. # ...