C++中i++和++i的区别

来源:互联网 发布:科比淘宝店铺叫什么 编辑:程序博客网 时间:2024/06/06 00:40

答:理论上++i更快,实际与编译器优化有关,通常几乎无差别。 
i++实现的代码为:

//i++实现代码为:                                    int operator++(int)                                  {    int temp = *this;                                         ++*this;                                                 return temp;                                    }//返回一个int型的对象本身
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

++i的实现代码:

// ++i实现代码为:int& operator++(){    *this += 1;    return *this;}//返回一个int型的对象引用
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

i++和++i的考点比较多,简单来说,就是i++返回的是i的值,而++i返回的是i+1的值。也就是++i是一个确定的值,是一个可修改的左值,如下使用:

    cout << ++(++(++i)) << endl;    cout << ++ ++i << endl;
  • 1
  • 2
  • 1
  • 2

可以不停的嵌套++i。 
这里有很多的经典笔试题,一起来观摩下:

int main(){    int i = 1;    printf("%d,%d\n", ++i, ++i);    //3,3    printf("%d,%d\n", ++i, i++);    //5,3    printf("%d,%d\n", i++, i++);    //6,5    printf("%d,%d\n", i++, ++i);    //8,9    system("pause");    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

首先是函数的入栈顺序从右向左入栈的,计算顺序也是从右往左计算的,不过都是计算完以后在进行的压栈操作: 
对于第5行代码,首先执行++i,返回值是i,这时i的值是2,再次执行++i,返回值是i,得到i=3,将i压入栈中,此时i为3,也就是压入3,3; 
对于第6行代码,首先执行i++,返回值是原来的i,也就是3,再执行++i,返回值是i,依次将3,5压入栈中得到输出结果 
对于第7行代码,首先执行i++,返回值是5,再执行i++返回值是6,依次将5,6压入栈中得到输出结果 
对于第8行代码,首先执行++i,返回i,此时i为8,再执行i++,返回值是8,此时i为9,依次将i,8也就是9,8压入栈中,得到输出结果。 
上面的分析也是基于vs搞的,不过准确来说函数多个参数的计算顺序是未定义的(the order of evaluation of function arguments are undefined)。笔试题目的运行结果随不同的编译器而异。 
参考:http://www.stroustrup.com/bs_faq2.html#evaluation-order 
下面是使用其他编译器输出的结果:

//------ C++ version ------#include <cstdio>int main(){    int i = 1;    printf("%d %d\n", ++i, ++i);    printf("%d %d\n", ++i, i++);    printf("%d %d\n", i++, i++);    printf("%d %d\n", i++, ++i);    return 0;}//------ C version ------#include <stdio.h>int main(){    int i = 1;    printf("%d %d\n", ++i, ++i);    printf("%d %d\n", ++i, i++);    printf("%d %d\n", i++, i++);    printf("%d %d\n", i++, ++i);    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

上面是参考的执行代码。 
gcc-5.1(C++) gcc-5.1(C++14) gcc-5.1(C) 的执行结果:

3 35 36 58 9
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

gcc-4.3.2(C++) clang-3.7(C++) clang-3.7(C) 的执行结果

2 34 45 67 9
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

gcc-5.1(C99 strict) 的编译结果:编译不通过。

C99 strict prog.c: In function 'main':prog.c:6:28: error: operation on 'i' may be undefined [-Werror=sequence-point]     printf("%d %d\n", ++i, ++i);                            ^prog.c:7:29: error: operation on 'i' may be undefined [-Werror=sequence-point]     printf("%d %d\n", ++i, i++);                             ^prog.c:8:29: error: operation on 'i' may be undefined [-Werror=sequence-point]     printf("%d %d\n", i++, i++);                             ^prog.c:9:28: error: operation on 'i' may be undefined [-Werror=sequence-point]     printf("%d %d\n", i++, ++i);                            ^cc1: all warnings being treated as errors
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

由上可见,这种比较毫无意义。

原创粉丝点击