【分析】

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. nodeJS基础08:读取图片

    1.读取图片 //server.js var http = require("http"); var readImage = require("./readImage&q ...

  2. 配置文件类 Properties

    Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息. Properties属于集合类,继承于Hashtable. Properties要注意的细节:    1. 如果配置文 ...

  3. 代码覆盖工具(gcov、lcov)的使用

    一.安装 gcov:是随gcc一起发布的,并不需要独立安装:lcov:其他博客说是随ltp发布的,结果下载下ltp之后编译了10多分钟,最后也没见lcov,最后到sourceforge下载了lcov单 ...

  4. eclipse的几个快捷键

    Alt / 自动补全 Ctrl Shift F 整理代码 Ctrl / 注释或取消注释 Ctrl 鼠标移到变量或类名上 转到声明/实现的接口/返回类型等 Ctrl Shift O import所有缺失 ...

  5. 链接rel属性external、nofollow、external nofollow三种写法的区别

    <script language="javascript" type="text/javascript" src="http://files.c ...

  6. CloudSim介绍和使用

    本文主要介绍一下我在使用CloudSim时翻译.整理和理解的一些信息,以及我的使用经验,希望能对有需要的朋友们有所帮助~1.我翻译和理解的一些信息:    2009年4月8日,澳大利亚墨尔本大学的网格 ...

  7. Django用已有的数据库

    python mysite/manage.py inspectdb  会按照你的数据库生成Model  拷贝进去用就可以了!

  8. VsFtpd配置文件详解

    1.默认配置:1>允许匿名用户和本地用户登陆.     anonymous_enable=YES     local_enable=YES2>匿名用户使用的登陆名为ftp或anonymou ...

  9. Hibernate JPA实体继承的映射(一) 概述

    http://www.cnblogs.com/yingsong/p/5179975.html   注:文章中据说的实体指的是被@Entity注解的类. JPA中对象关系映射通常情况下是一个实体对应一个 ...

  10. BZOJ1483: [HNOI2009]梦幻布丁

    传送门 名字起得很高端实际上很简单的算法hhh 启发式合并 简单讲就是一些合并一堆队列的题可以用启发式合并,或者说这是一个思想.每次把小的合并到大的部分,均摊复杂度$O(MlogN)$. //BZOJ ...