由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个简单coding问题
相关主题
Why I can't compile this function successfully请问关于overloading << (转载)
C++ Q42: (C22)请教一个c的概念题
面完G的电面了,忐忑const_reverse_iterator和reverse_iterator有什么区别?
One C++ question问个c++题
求教:这个程序为什么不能编译?弱问个C++ 问题 (const_cast)
one C++ question问个面试题
C++ Q83: 这个const_cast什么意思?问个超级小问题
请教C/C++小问个C++ virtual function的问题
相关话题的讨论汇总
话题: cur话题: iterator话题: int话题: cout话题: endl
进入JobHunting版参与讨论
1 (共1页)
n****e
发帖数: 10
1
class Iterator {
public:
Iterator() : cur(0) {};
bool hasNext() {
return cur < 20;
};
int next() {
++cur;
return cur;
};
private:
int cur;
};
int main() {
Iterator it;
int a = it.next();
int b = it.next();
cout << a << " " << b << endl;
cout << it.next() << " " << it.next() << endl;

为啥输出是
1 2
4 3
而不是
1 2
3 4
f*******w
发帖数: 1243
2
因为cout是从右往左读到buffer里面,然后再从左往右输出的
所以先执行后面那个next()而不是前面那个
M********0
发帖数: 1230
3
编译器的问题
gcc 给出的是
1 2
4 3
clang和icpc给出的是
1 2
3 4
f*******w
发帖数: 1243
4
这个还真不知道……涨姿势了

【在 M********0 的大作中提到】
: 编译器的问题
: gcc 给出的是
: 1 2
: 4 3
: clang和icpc给出的是
: 1 2
: 3 4

t**8
发帖数: 4527
5
nonsense question

【在 n****e 的大作中提到】
: class Iterator {
: public:
: Iterator() : cur(0) {};
: bool hasNext() {
: return cur < 20;
: };
: int next() {
: ++cur;
: return cur;
: };

n****e
发帖数: 10
6
Thanks for all the answers. It does not have much algorithmic stuff, but
reminds me of the arguments pushing order of functions, and different
implementation choices for different compilers.
n****e
发帖数: 10
7
I've found this, for anyone who's interested.
http://en.cppreference.com/w/cpp/language/eval_order
1 (共1页)
进入JobHunting版参与讨论
相关主题
问个C++ virtual function的问题求教:这个程序为什么不能编译?
问个缺少逗号的数组赋值问题one C++ question
问个c++的问题C++ Q83: 这个const_cast什么意思?
问个G的intern面试请教C/C++小
Why I can't compile this function successfully请问关于overloading << (转载)
C++ Q42: (C22)请教一个c的概念题
面完G的电面了,忐忑const_reverse_iterator和reverse_iterator有什么区别?
One C++ question问个c++题
相关话题的讨论汇总
话题: cur话题: iterator话题: int话题: cout话题: endl