【分析】

i++与++i哪个效率更高?

(1)在内建数据类型的情况下,效率没有区别;

(2)在自定义数据类型Class的情况下,++i效率更高!

自定义数据类型的情况下:++i返回对象的引用;i++总是要创建一个临时对象,在退出函数时还要销毁它,而且返回临时对象的值时还会调用其拷贝构造函数。

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;

class Integer
{
public:
    Integer(int value): v(value)
    {
        cout << "default constructor" << endl;
    }
    Integer(const Integer &other)
    {
        cout << "copy constructor" << endl;
        v = other.v;
    }
    Integer &operator=(const Integer &other)
    {
        cout << "copy assignment" << endl;
        v = other.v;
        return *this;
    }

// ++i  first +1,then return new value
    Integer &operator++()
    {
        cout << "Integer::operator++()" << endl;
        v++;
        return *this;
    }

// i++  first save old value,then +1,last return old value
    Integer operator++(int)
    {
        cout << "Integer::operator++(int)" << endl;
        Integer old = *this;
        v++;
        return old;
    }

void output()
    {
        cout << "value " << v << endl;
    }
private:
    int v;
};

void test_case()
{
    Integer obj();
    Integer obj2 = obj;
    Integer obj3();
    obj3 = obj;
    cout << "--------------" << endl;
    cout << "++i" << endl;
    ++obj;
    obj.output();
    cout << "i++" << endl;
    obj++;
    obj.output();
}

int main()
{
    test_case();
    ;
}
/*
default constructor
copy constructor
default constructor
copy assignment
--------------
++i
Integer::operator++()
value 11
i++
Integer::operator++(int)
copy constructor
value 12
*/

随机推荐

  1. 栈的理解以及如何计算程序所需栈的大小并在IAR中设置栈

    文章首发于浩瀚先森博客 #栈的理解 一个程序大体上讲都是由变量和函数组合而成,变量有全局变量和局部变量,还有函数间传值的参数以及返回值. Stack是为了程序运行过程中临时保存所需数据而在内存里分配的 ...

  2. 使用apache ftpserver搭建ftp服务器

    作为一个javaer,遇到任何问题,先查一下java中的解决方案.地球上的许多事情,在java中都能找到完美的解决方案.之前搭建ftp服务器使用的是vsftpd,现在可以把它卸掉了,它以服务的形式运行 ...

  3. java图形处理-Java Graphics2D

    java.awt 类 Graphics2D java.lang.Object 继承者 java.awt.Graphics 继承者 java.awt.Graphics2D public abstract ...

  4. C#-WebForm-ASP开发练习:从数据库中动态添加信息

    传统的ASP开发方式,是C#代码和HTML代码混合在一起,ASP与ASP.NET不是一个东西. <%  %>  -  可以扩起来一段范围,这一段范围之内只能允许编写C#代码 <%= ...

  5. js兼容获取元素的样式

    js获取元素的样式的兼容性处理: function getStyle(obj,attr){ return obj.currentStyle?obj.currentStyle[attr]:getComp ...

  6. awk中的system和getline的用法

    system只能对命令的输出结果输出到终端. getline在awk中可以使命令的输出结果传到一个变量中保存. # awk 'BEGIN{system("date")|getlin ...

  7. VOC2007检测任务的评估标准

    VOC2007数据集使用mAP值作为检测算法检测结果的性能评估得分.mAP意思是mean Average Precision,Precision是指精度,Average Precision是指11个等 ...

  8. 时间戳 时区 java mysql

    当一个时间 比如2016年5月6日,生成时间戳.这个运算是与时区有关的.首先得确认这个时间是哪个时区的,然后转换成utc时区的时间.再减去1970,得到的秒数,就是时间戳. 时间戳是个一定的值,他与时 ...

  9. windows bat 设置代理上网脚本bat

    取消IE代理服务器 ****************************************************************************************** ...

  10. ArcGIS Server开发教程系列(7)使用ArcGIS API for Javascript-Hello World

    ArcGIS API for Javascript  API下载地址:http://support.esrichina-bj.cn/2011/0223/960.html 选择最新的下载就好了,目前是3 ...