由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 有个SB interviewer和我说++i比i++好 (转载)
相关主题
Overridden function will cause function shadow in C++, but not in Javalambda 什么时候进入 c++的?
问个指针array 的简单问题java 8就是一坨屎
C++ Q110: Add without +今晚还编程
[合集] 问个土问题 printf, 别PengC/C++函数调用和栈内存
很不习惯cin/cout 请问c++为什么会编译失败?
set operation in c谁来解释一下这个是compiler问题吗?
一个搞统计的对C#的第一印象a C++ interview question..........
有没有大牛说说C里边for循环的坏处C++格式输出
相关话题的讨论汇总
话题: test话题: r3话题: operator话题: c++话题: int
进入Programming版参与讨论
1 (共1页)
s******n
发帖数: 3946
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: swanswan (swan), 信区: JobHunting
标 题: 有个SB interviewer和我说++i比i++好
发信站: BBS 未名空间站 (Thu Mar 22 16:09:09 2012, 美东)
他的意思是假设是operator重载
++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
设编译无优化)
大家看有道理吗?
I did a real test on arm compiler turn off optimization:
the O0 code is exactly the same, except that post operator++()(int dummy)
has an extra dummy parameter which is required by c++ to identify the
difference of prefix and postfix.
2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
printf("%d \n", 10+ (abc++).value);
sub r3, fp, #8
mov r0, r3
mov r1, #0 --> the dummy parameter of postfix
bl _ZN4TestppEi --> call the overload function
mov r3, r0
ldr r3, [r3, #0]
add r3, r3, #10
ldr r0, .L12
mov r1, r3
bl printf
printf("%d \n", 20+ (++abc).value);
sub r3, fp, #8
mov r0, r3
bl _ZN4TestppEv --> call the overload function
mov r3, r0
ldr r3, [r3, #0]
add r3, r3, #20
ldr r0, .L12
mov r1, r3
bl printf
c++ source code
#include
class Test {
public:
Test(int v):value(v){};
Test():value(0){};
int value;
int operator()();
Test& operator++();
Test& operator++(int postVersion);
};
Test& Test::operator++(){
value++;
return *this;
}
Test& Test::operator++(int postVersion){
value++;
return *this;
}
int Test::operator()() {
return value;
}
int main(int argc, char** argv)
{
Test abc(100);
printf("%d \n", 10+ (abc++).value);
printf("%d \n", 20+ (++abc).value);
}
S*********g
发帖数: 5298
2
谁敢来同意这个interviewer啊
一不小心就被楼主封为SB网友了

【在 s******n 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: swanswan (swan), 信区: JobHunting
: 标 题: 有个SB interviewer和我说++i比i++好
: 发信站: BBS 未名空间站 (Thu Mar 22 16:09:09 2012, 美东)
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)

p***o
发帖数: 1252
3
We don't agree or disagree with the interviewer.
But LZ's code of post++ is clearly wrong. So ...

【在 S*********g 的大作中提到】
: 谁敢来同意这个interviewer啊
: 一不小心就被楼主封为SB网友了

t****t
发帖数: 6806
4
你在job版不是已经得到答案了吗? 不用这里再重复一遍了吧?

【在 s******n 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: swanswan (swan), 信区: JobHunting
: 标 题: 有个SB interviewer和我说++i比i++好
: 发信站: BBS 未名空间站 (Thu Mar 22 16:09:09 2012, 美东)
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)

t****t
发帖数: 6806
5
哦, 这个我还没有注意到. 总之SB的不是interviewer...

【在 p***o 的大作中提到】
: We don't agree or disagree with the interviewer.
: But LZ's code of post++ is clearly wrong. So ...

s******n
发帖数: 3946
6
wuuu,被嘲笑了,当我SB吧,可是即使修改了post++,还是一样的汇编代码长度。
t****t
发帖数: 6806
7
how did you modify your code?

【在 s******n 的大作中提到】
: wuuu,被嘲笑了,当我SB吧,可是即使修改了post++,还是一样的汇编代码长度。
s******n
发帖数: 3946
8
Test Test::operator++(int postVersion)
{
Test temp = a;
value++;
return temp;
}
用了临时变量temp后,caller的代码还是一样,不过function代码确实长了,没注意这
个,真SB了。

【在 t****t 的大作中提到】
: how did you modify your code?
t****t
发帖数: 6806
9
原来你只比较caller...

【在 s******n 的大作中提到】
: Test Test::operator++(int postVersion)
: {
: Test temp = a;
: value++;
: return temp;
: }
: 用了临时变量temp后,caller的代码还是一样,不过function代码确实长了,没注意这
: 个,真SB了。

a9
发帖数: 21638
10
新手入门哈。++i比i++省资源不是共识吗?

【在 s******n 的大作中提到】
: Test Test::operator++(int postVersion)
: {
: Test temp = a;
: value++;
: return temp;
: }
: 用了临时变量temp后,caller的代码还是一样,不过function代码确实长了,没注意这
: 个,真SB了。

相关主题
set operation in clambda 什么时候进入 c++的?
一个搞统计的对C#的第一印象java 8就是一坨屎
有没有大牛说说C里边for循环的坏处今晚还编程
进入Programming版参与讨论
h*******s
发帖数: 8454
11
整形的话有区别么?

【在 a9 的大作中提到】
: 新手入门哈。++i比i++省资源不是共识吗?
k*******d
发帖数: 1340
12
int的话一般没区别,也看具体硬件,有的硬件有针对性的优化
object就是++i好了,共识,我面试就被问过这个
h*******s
发帖数: 8454
13
这个是常识吧,好像effective c++之类的地方提到过

【在 k*******d 的大作中提到】
: int的话一般没区别,也看具体硬件,有的硬件有针对性的优化
: object就是++i好了,共识,我面试就被问过这个

p*********t
发帖数: 2690
14
re
++i把i加了1之后,用新的i的值就好了,i的旧值不用管了;
i++要先把i的旧值存起来待以后用,然后用i的旧值做运算,完事后,要把i的旧值加1.
这个逻辑确实比++i复杂多了。

【在 k*******d 的大作中提到】
: int的话一般没区别,也看具体硬件,有的硬件有针对性的优化
: object就是++i好了,共识,我面试就被问过这个

r*********r
发帖数: 3195
15
谦虚点, 再煞笔的人最后好歹都能找个工作.
p*********t
发帖数: 2690
16
这说明c++的水真的太深了。

【在 r*********r 的大作中提到】
: 谦虚点, 再煞笔的人最后好歹都能找个工作.
m*********t
发帖数: 527
17
不过 compiler 是会优化的,也就是写 ++i 和 i++ compiler 生成的 assembly 可能
都是一样的(gcc 应该就自动做了)。当然,这个是可能,因为 compiler 完全可以不
这么做。。。。。

1.

【在 p*********t 的大作中提到】
: re
: ++i把i加了1之后,用新的i的值就好了,i的旧值不用管了;
: i++要先把i的旧值存起来待以后用,然后用i的旧值做运算,完事后,要把i的旧值加1.
: 这个逻辑确实比++i复杂多了。

n******t
发帖数: 4406
18
int 的话应该被又花掉了。。
如果是C++的object,就++i把。

【在 s******n 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: swanswan (swan), 信区: JobHunting
: 标 题: 有个SB interviewer和我说++i比i++好
: 发信站: BBS 未名空间站 (Thu Mar 22 16:09:09 2012, 美东)
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)

a*w
发帖数: 4495
19
其他语言没有这个问题?

【在 p*********t 的大作中提到】
: 这说明c++的水真的太深了。
r*********r
发帖数: 3195
20
c++ 开放了 constructor, destructor, operator overloading,
这三点基本上把oo 编译器的内部机制开放给程序员了。没有其他语言能做到这一点。

【在 a*w 的大作中提到】
: 其他语言没有这个问题?
相关主题
C/C++函数调用和栈内存a C++ interview question..........
请问c++为什么会编译失败?C++格式输出
谁来解释一下这个是compiler问题吗?c++ 中如何把str转换为float?
进入Programming版参与讨论
a****n
发帖数: 1887
21
int 的话, 基本没区别, 放在表达式里,
比如
1. int x = ++i + y;
2. int x = i++ + y;
2 的 eval 还要快一点,
如果是class的话, ++i 比 i++ 快一点。
r*********r
发帖数: 3195
22
快多少不是问题。关键是这两个操作符的 semantic 不同。
++x 不做拷贝。
x++ 拷贝对象,返回新的object, ++作用在原来的object上。
快多快少取决于拷贝这个object 要花多少时间。
B*****g
发帖数: 34098
23
coask for java

【在 a*w 的大作中提到】
: 其他语言没有这个问题?
g*****g
发帖数: 34805
24
I never write ++i in any java code. I doubt it makes
any difference. And even if it does, it's negligible.

【在 B*****g 的大作中提到】
: coask for java
v*****r
发帖数: 1119
25
semantic difference betwwen ++i, i++ in java is the same as in c. (In c++,
it is more complicated as people can overload ++ operator).
In my opinion, in java or c, ++i should always be used instead of i++ since
its semantic is aligned with human logic even when coders don't know the
difference.

【在 B*****g 的大作中提到】
: coask for java
s******8
发帖数: 4192
26
要用值就要copy,不管是++x还是x++。实际是一样的。
++x,++作用在原来的object上,拷贝对象,返回object。
x++ 拷贝对象,返回object, ++作用在原来的object上。

【在 r*********r 的大作中提到】
: 快多少不是问题。关键是这两个操作符的 semantic 不同。
: ++x 不做拷贝。
: x++ 拷贝对象,返回新的object, ++作用在原来的object上。
: 快多快少取决于拷贝这个object 要花多少时间。

r*********r
发帖数: 3195
27
谁说一定要 return value 了。知道什么是 side effect 吗。

【在 s******8 的大作中提到】
: 要用值就要copy,不管是++x还是x++。实际是一样的。
: ++x,++作用在原来的object上,拷贝对象,返回object。
: x++ 拷贝对象,返回object, ++作用在原来的object上。

a****y
发帖数: 2548
28
这两段code结果根本不一样,一个的结果是(i+1+y),一个是(i+y),怎么会没有区别?

【在 a****n 的大作中提到】
: int 的话, 基本没区别, 放在表达式里,
: 比如
: 1. int x = ++i + y;
: 2. int x = i++ + y;
: 2 的 eval 还要快一点,
: 如果是class的话, ++i 比 i++ 快一点。

g*****g
发帖数: 34805
29
You are wrong. ++i or i++ is not important at all.
Even if ++i can save one operation, it won't make any difference
If it makes any difference, below has to happen
1. It has to be in a loop
2. The loop has to be the bottleneck in the system.
3. The loop has also to be very very simple so one
assembly operation consists of fair percentage of
the loop running. At least 1% or it's trivial.
With above assumptions, you are looking at a program
that has nothing heavy but this loop, and this loop is also
very very simple. You are looking at a HelloWorld loop
that runs a million time. And you can't actually print
HelloWorld since that's too heavy an operation compared
to the difference above.
I think I've got my point crossed. Coding is all about
getting best readablity, optimization comes where it's
needed, and it's needed on your pain-point. Even then,
it's all about algorithm, architecture, and rarely assembly
level operations. Premature optimization is evil.
My advice is always leaving i++ or ++i in its own line.

,
since

【在 v*****r 的大作中提到】
: semantic difference betwwen ++i, i++ in java is the same as in c. (In c++,
: it is more complicated as people can overload ++ operator).
: In my opinion, in java or c, ++i should always be used instead of i++ since
: its semantic is aligned with human logic even when coders don't know the
: difference.

N***m
发帖数: 4460
30
agree, at least for java app.

【在 g*****g 的大作中提到】
: You are wrong. ++i or i++ is not important at all.
: Even if ++i can save one operation, it won't make any difference
: If it makes any difference, below has to happen
: 1. It has to be in a loop
: 2. The loop has to be the bottleneck in the system.
: 3. The loop has also to be very very simple so one
: assembly operation consists of fair percentage of
: the loop running. At least 1% or it's trivial.
: With above assumptions, you are looking at a program
: that has nothing heavy but this loop, and this loop is also

相关主题
two general C++ question问个指针array 的简单问题
问一个C++下的Bug(Linux下)C++ Q110: Add without +
Overridden function will cause function shadow in C++, but not in Java[合集] 问个土问题 printf, 别Peng
进入Programming版参与讨论
v*****r
发帖数: 1119
31
再解释一下我用 ++i over i++ 的原因.
最主要的原因很简单,就是我前面说的 ++i 没有绕弯子,it behave exactly what
most beginners guess what it should behave. i++ 饶了个小弯,虽然对你不是什么
问题,但还是让很多人感觉不自然(这么多人还问这个简单的问题就说明了这点).
From code readability point of view, I would like to just use ++i. i++就是一
鸡肋的 syntax sugar in c or java.
和你观点一样,我不认为 performance 是 ++i over i++ 的原因。
1. Like you said, it is very rare i++ cause performance issue even with that
extra step.
2. Modern compiler 会对 i++ 做优化,, 当发现 i++ 没有 involved in assignment
or complex expression 时,it will just remove the extra copy step.
我觉得我们说的并没有冲突,也就是 coding 的习惯不一样

【在 g*****g 的大作中提到】
: You are wrong. ++i or i++ is not important at all.
: Even if ++i can save one operation, it won't make any difference
: If it makes any difference, below has to happen
: 1. It has to be in a loop
: 2. The loop has to be the bottleneck in the system.
: 3. The loop has also to be very very simple so one
: assembly operation consists of fair percentage of
: the loop running. At least 1% or it's trivial.
: With above assumptions, you are looking at a program
: that has nothing heavy but this loop, and this loop is also

p*********t
发帖数: 2690
32
这个"sb"肯定是90年代那拨人,狠重视代码的简洁。那个时候内存狠小,大家都狠重视
代码的简洁,觉得越短越好,size matters,不重视代码的可维护性,写代码的人走了
,维护起来就狠麻烦。
現在情况完全不同了,内存都是10g左右了,代码稍微长点,运算差别在普通用户来讲
,没什么差别,倒是因为跳槽太普遍,代码的可维护性要求狠高了,代码的注释是必须
的,否则后来维护的人看不懂,怎么维护?

【在 s******n 的大作中提到】
: Test Test::operator++(int postVersion)
: {
: Test temp = a;
: value++;
: return temp;
: }
: 用了临时变量temp后,caller的代码还是一样,不过function代码确实长了,没注意这
: 个,真SB了。

g*****g
发帖数: 34805
33
As I said, if you are not using it in for clause,
leave ++i or i++ in its own line. And it's all set.
There's no confusion.
i++ has better readability, as more examples are using
that and people feel more comfortable with it.

that
assignment

【在 v*****r 的大作中提到】
: 再解释一下我用 ++i over i++ 的原因.
: 最主要的原因很简单,就是我前面说的 ++i 没有绕弯子,it behave exactly what
: most beginners guess what it should behave. i++ 饶了个小弯,虽然对你不是什么
: 问题,但还是让很多人感觉不自然(这么多人还问这个简单的问题就说明了这点).
: From code readability point of view, I would like to just use ++i. i++就是一
: 鸡肋的 syntax sugar in c or java.
: 和你观点一样,我不认为 performance 是 ++i over i++ 的原因。
: 1. Like you said, it is very rare i++ cause performance issue even with that
: extra step.
: 2. Modern compiler 会对 i++ 做优化,, 当发现 i++ 没有 involved in assignment

h**********c
发帖数: 4120
34
I remember it is OS class stuff or something call hardware system or system
hardware comp 229, this seems to relate to CPU design.
Each tick of CPU for example, take one operand, take one operator.
++i take one operand, one operator.
i++ take two operands, one operator, thus take one more tick.
Not sure. in fact I always write i++ cause c++.
h**********c
发帖数: 4120
35
try to think again
++i, prefetch the return address
i++ need operand value and one operand result, two address, thus two ticks.
Again not sure. Please verify.
a9
发帖数: 21638
36
也得分情况啊。
比如
int i=1;
int b=i++;和int b =++i;可是完全不同的东西了。

【在 m*********t 的大作中提到】
: 不过 compiler 是会优化的,也就是写 ++i 和 i++ compiler 生成的 assembly 可能
: 都是一样的(gcc 应该就自动做了)。当然,这个是可能,因为 compiler 完全可以不
: 这么做。。。。。
:
: 1.

a9
发帖数: 21638
37
各种语言应该都是一样的。++i和i++的用法不同啊。

【在 g*****g 的大作中提到】
: I never write ++i in any java code. I doubt it makes
: any difference. And even if it does, it's negligible.

a****l
发帖数: 8211
38
this is exactly why goodbug said "leave i++/++i in its own line". There is
absolutely nothing that can be saved by combining it with other things.
Maybe just a few bytes in the source code, who cares? Saving few bytes in ahard drive with 1TB storage?
BTW, there is no point talking about "2 operands vs. 1 operand" at the level of source code. Optimization changes everything.

【在 a9 的大作中提到】
: 也得分情况啊。
: 比如
: int i=1;
: int b=i++;和int b =++i;可是完全不同的东西了。

x****u
发帖数: 44466
39
假设编译不优化的话,还是用basic吧。

【在 s******n 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: swanswan (swan), 信区: JobHunting
: 标 题: 有个SB interviewer和我说++i比i++好
: 发信站: BBS 未名空间站 (Thu Mar 22 16:09:09 2012, 美东)
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)

p********a
发帖数: 535
40
++i is never worse than i++, so better use ++i in all possible situations
just for consistency.

【在 s******n 的大作中提到】
: Test Test::operator++(int postVersion)
: {
: Test temp = a;
: value++;
: return temp;
: }
: 用了临时变量temp后,caller的代码还是一样,不过function代码确实长了,没注意这
: 个,真SB了。

相关主题
[合集] 问个土问题 printf, 别Peng一个搞统计的对C#的第一印象
很不习惯cin/cout有没有大牛说说C里边for循环的坏处
set operation in clambda 什么时候进入 c++的?
进入Programming版参与讨论
p*********t
发帖数: 2690
41
i++,++i还是尽量少用的好,和指针一样,都是狠容易出错,bug狠难找的那种。把一行
code写成2行,是笨办法,但是永远不会有bug.

【在 a9 的大作中提到】
: 也得分情况啊。
: 比如
: int i=1;
: int b=i++;和int b =++i;可是完全不同的东西了。

h**********c
发帖数: 4120
42
Take some time to review the books.
Thanking in C++ TIC2Vone.pdf
P517 on operator overloading.
P388 on return const, built in type or user define data type.
So you see the difference (gap) between the interviewer and viewee.
Please don't use sensational words.
While I'd like to the difference of assmebly code.
a9
发帖数: 21638
43
那你for怎么写啊?

【在 p*********t 的大作中提到】
: i++,++i还是尽量少用的好,和指针一样,都是狠容易出错,bug狠难找的那种。把一行
: code写成2行,是笨办法,但是永远不会有bug.

L***n
发帖数: 6727
44
i=i+1?

【在 a9 的大作中提到】
: 那你for怎么写啊?
T******r
发帖数: 257
45
特烦把++写到表达式里的, 不管是前置或者后置.
s******8
发帖数: 4192
46
同意把++写进表达式的都是SB。code不出bug,容易维护才是才是最重要的。
s******8
发帖数: 4192
47
还是用i++。就是说++不要嵌入表达式,单独没关系。++i,i++没有歧义。
x=i++;应该写x=i;i++;
x=++i;应该写++i;x=i;

【在 a9 的大作中提到】
: 那你for怎么写啊?
h**********c
发帖数: 4120
48
The point of i++ and ++i
is that old c-school geeks
can daisy-chain them. Only
GDM cool geeks write/read such.
p*********t
发帖数: 2690
49
re
++i,i++这种就是geek搞的小聪明,还是少用为好,尤其是这种
a[i++] = a[--i];

【在 h**********c 的大作中提到】
: The point of i++ and ++i
: is that old c-school geeks
: can daisy-chain them. Only
: GDM cool geeks write/read such.

h**********c
发帖数: 4120
50
I could not agree you totally. Even geeks need career safety pin. You know
what I mean.

【在 p*********t 的大作中提到】
: re
: ++i,i++这种就是geek搞的小聪明,还是少用为好,尤其是这种
: a[i++] = a[--i];

相关主题
java 8就是一坨屎 请问c++为什么会编译失败?
今晚还编程谁来解释一下这个是compiler问题吗?
C/C++函数调用和栈内存a C++ interview question..........
进入Programming版参与讨论
w*********l
发帖数: 1337
51
这种写法不合标准。=不是sequence point。写的不是geeky不geeky的问题,是正确不
正确的问题。

【在 p*********t 的大作中提到】
: re
: ++i,i++这种就是geek搞的小聪明,还是少用为好,尤其是这种
: a[i++] = a[--i];

h**********c
发帖数: 4120
52
first know sequence point, can you give a tutor.

【在 w*********l 的大作中提到】
: 这种写法不合标准。=不是sequence point。写的不是geeky不geeky的问题,是正确不
: 正确的问题。

h*******s
发帖数: 8454
53
印象中水木cplusplus版faq里面第一条就是关于这个的

【在 h**********c 的大作中提到】
: first know sequence point, can you give a tutor.
a*****i
发帖数: 268
54
If you know exactly what you are doing, then using either one is fine.
If you just don't want to care their difference, it's better use ++i.
g*****g
发帖数: 34805
55
It's not about if you know what you are doing, but
if everybody knows what you are doing.
Everyone should use i++ in its own line, to give yourself
and your teammates a break. Coding is all about readablity,
not about being cute.

【在 a*****i 的大作中提到】
: If you know exactly what you are doing, then using either one is fine.
: If you just don't want to care their difference, it's better use ++i.

d****p
发帖数: 685
56

~~~ are you the code police in your team? What if all other teammates use
++i instead of i++?

【在 g*****g 的大作中提到】
: It's not about if you know what you are doing, but
: if everybody knows what you are doing.
: Everyone should use i++ in its own line, to give yourself
: and your teammates a break. Coding is all about readablity,
: not about being cute.

d****p
发帖数: 685
57

~~~ agree. BOOST_FOREACH, std foreach, for/iterator, for/subscript, while,
do/while, as long as you make a reasonable choice, it should be all fine.

【在 a*****i 的大作中提到】
: If you know exactly what you are doing, then using either one is fine.
: If you just don't want to care their difference, it's better use ++i.

g*****g
发帖数: 34805
58
++i or i++ is not the problem. You have to have that agreed in the team.
And yes, we do have peer review on the code. I'll ask a change if there's
any confusing statements like that.

use

【在 d****p 的大作中提到】
:
: ~~~ agree. BOOST_FOREACH, std foreach, for/iterator, for/subscript, while,
: do/while, as long as you make a reasonable choice, it should be all fine.

d****p
发帖数: 685
59

I believe you are making some senses here. Yes, your code has to pass code
review and be understandable to your teammates. What is confusing to me in
your previous post is the statement about "you should use i++ in a separate
line" blahblah. I suppose it is about C++ code and I have never encountered
a decent C++ coder who thinks ++i is confusing.
May I ask a change in your previous post? I know posting on here doesn't
have to be peer reviewed but such a change will be helpful to beginners here
seeking useful advice.

【在 g*****g 的大作中提到】
: ++i or i++ is not the problem. You have to have that agreed in the team.
: And yes, we do have peer review on the code. I'll ask a change if there's
: any confusing statements like that.
:
: use

g*****g
发帖数: 34805
60
I recommend i++ over ++i since most programmers prefer it that way.
You should see way more examples using i++ than ++i. Since ++i doesn't
give you any benefit, why do you want to use that?
In a for loop, i++ or ++i is essentially in its own line.
And remember, not every C++ developer is decent, same for programmers
of other language. Do yourself a favor.

separate
encountered
here

【在 d****p 的大作中提到】
:
: I believe you are making some senses here. Yes, your code has to pass code
: review and be understandable to your teammates. What is confusing to me in
: your previous post is the statement about "you should use i++ in a separate
: line" blahblah. I suppose it is about C++ code and I have never encountered
: a decent C++ coder who thinks ++i is confusing.
: May I ask a change in your previous post? I know posting on here doesn't
: have to be peer reviewed but such a change will be helpful to beginners here
: seeking useful advice.

相关主题
C++格式输出问一个C++下的Bug(Linux下)
c++ 中如何把str转换为float?Overridden function will cause function shadow in C++, but not in Java
two general C++ question问个指针array 的简单问题
进入Programming版参与讨论
d****p
发帖数: 685
61
When you see i++ or ++i, i could be of any type, not just integer. When two
statements have different semantics, their difference matters in certain
scenarios and it is a must to know when to use which.
One of them happens to appear more often doesn't justify ignoring their
difference.
If you don't know something, it is just fine - employ a good attitude and
learn something. It doesn't make much sense to just close your eyes.
And it makes virtually no sense to comment something you are not familiar
with.

【在 g*****g 的大作中提到】
: I recommend i++ over ++i since most programmers prefer it that way.
: You should see way more examples using i++ than ++i. Since ++i doesn't
: give you any benefit, why do you want to use that?
: In a for loop, i++ or ++i is essentially in its own line.
: And remember, not every C++ developer is decent, same for programmers
: of other language. Do yourself a favor.
:
: separate
: encountered
: here

V********n
发帖数: 3061
62
还真是第一次听说++i比i++有更高的可读性,更自然。我怎么觉得99%以上的程序员都
会觉得i++更亲切啊!?

that
assignment

【在 v*****r 的大作中提到】
: 再解释一下我用 ++i over i++ 的原因.
: 最主要的原因很简单,就是我前面说的 ++i 没有绕弯子,it behave exactly what
: most beginners guess what it should behave. i++ 饶了个小弯,虽然对你不是什么
: 问题,但还是让很多人感觉不自然(这么多人还问这个简单的问题就说明了这点).
: From code readability point of view, I would like to just use ++i. i++就是一
: 鸡肋的 syntax sugar in c or java.
: 和你观点一样,我不认为 performance 是 ++i over i++ 的原因。
: 1. Like you said, it is very rare i++ cause performance issue even with that
: extra step.
: 2. Modern compiler 会对 i++ 做优化,, 当发现 i++ 没有 involved in assignment

c****p
发帖数: 6474
63
因为for里面都是i++吧

【在 V********n 的大作中提到】
: 还真是第一次听说++i比i++有更高的可读性,更自然。我怎么觉得99%以上的程序员都
: 会觉得i++更亲切啊!?
:
: that
: assignment

g*****g
发帖数: 34805
64
I am senior enough to know the pitfalls in programming languages
and give some advices.
I couldn't care less what you think.

two

【在 d****p 的大作中提到】
: When you see i++ or ++i, i could be of any type, not just integer. When two
: statements have different semantics, their difference matters in certain
: scenarios and it is a must to know when to use which.
: One of them happens to appear more often doesn't justify ignoring their
: difference.
: If you don't know something, it is just fine - employ a good attitude and
: learn something. It doesn't make much sense to just close your eyes.
: And it makes virtually no sense to comment something you are not familiar
: with.

r*********r
发帖数: 3195
65
java 没有 operator overloading, 所以 senior java coder 的 advice 不算数.
现在的 c++ 里, 推荐用 ++i. for-loop 里也应该用 ++i, 而不是用 i++,
因为 i 很可能是 iterator, 而不是 integer.
除非有特殊原因, 一律用 ++i.
make c++ the only exception. :->


【在 g*****g 的大作中提到】
: I am senior enough to know the pitfalls in programming languages
: and give some advices.
: I couldn't care less what you think.
:
: two

r***e
发帖数: 1840
66
实在忍不住潜水说几句,开始几位已经说得很清楚的。problem is when doing i++
and ++i on an object!! 既然还有人扯到agree within the team. 这是行为i艺术吗
?LZ在面试,interviewer说你错了,你马上连声说“您老说得对。”公司就会招你?
r***e
发帖数: 1840
67
btw java does't have operator overloading, don't bother
N***m
发帖数: 4460
68
java 8 will add operator overloading

【在 r***e 的大作中提到】
: btw java does't have operator overloading, don't bother
r***e
发帖数: 1840
69
Even so, the compiler and JVM controls everything in Java. And considering
the Java performance, the difference will not be that big as in C++. Of
course it's my guessing.
Btw I don't think java should do this. Java should just be a easy to use
version of c++.

【在 N***m 的大作中提到】
: java 8 will add operator overloading
y***d
发帖数: 2330
70
that will be very silly....

【在 N***m 的大作中提到】
: java 8 will add operator overloading
相关主题
问个指针array 的简单问题很不习惯cin/cout
C++ Q110: Add without +set operation in c
[合集] 问个土问题 printf, 别Peng一个搞统计的对C#的第一印象
进入Programming版参与讨论
x*******1
发帖数: 28835
71
回字的108种写法,你们又figure out了。景仰。。。
1 (共1页)
进入Programming版参与讨论
相关主题
C++格式输出很不习惯cin/cout
c++ 中如何把str转换为float?set operation in c
two general C++ question一个搞统计的对C#的第一印象
问一个C++下的Bug(Linux下)有没有大牛说说C里边for循环的坏处
Overridden function will cause function shadow in C++, but not in Javalambda 什么时候进入 c++的?
问个指针array 的简单问题java 8就是一坨屎
C++ Q110: Add without +今晚还编程
[合集] 问个土问题 printf, 别PengC/C++函数调用和栈内存
相关话题的讨论汇总
话题: test话题: r3话题: operator话题: c++话题: int