C++ 这里怎么了?结合性?评估顺序?如何更改订单?

C++ 这里怎么了?结合性?评估顺序?如何更改订单?,c++,C++,流插入运算符的关联性是rtl,忘记这一事实有时会导致运行时或逻辑错误。 例如: 第一- 在主要功能中: //....here is main() cout<<”1st=”<<F()<<”,2nd=”<<F()<<”,3rd=”<<F(); 这与我们第一眼看到的不同 第二- 假设我们有一个堆栈数据结构的实现,如下所示: // //... a Stack<DataType> class ……

流插入运算符的关联性是rtl,忘记这一事实有时会导致运行时或逻辑错误。 例如:

第一-

在主要功能中:

//....here is main()
cout<<”1st=”<<F()<<”,2nd=”<<F()<<”,3rd=”<<F();
这与我们第一眼看到的不同

第二- 假设我们有一个堆栈数据结构的实现,如下所示:

    //
    //... a Stack<DataType> class …… 
    //

    Stack<int> st(10);
    for(int i=1;i<11;i++)
       st.push(i);

cout<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl;
但我们有:

7
8
9
10

没有的内部错误,没有。但我认为你可能把联想性和求值顺序弄混了。指定求值顺序的唯一运算符是&、| |和(逗号)。当你说:

cout<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl;

cout唯一正确关联的是赋值运算符。见本标准§5.4至5.18。
要了解这是一个评估顺序问题而不是关联性问题,请将代码修改为:

int a = st.pop();
int b = st.pop();
int c = st.pop();
cout << a << endl << b << endl << c << endl;
inta=st.pop();
int b=圣普();
int c=圣普();
库特
7
8
9
10
cout<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl;
int a = st.pop();
int b = st.pop();
int c = st.pop();
cout << a << endl << b << endl << c << endl;