C++实参列表从右往左算
  • 板块灌水区
  • 楼主__RelativitY__
  • 当前回复4
  • 已保存回复4
  • 发布时间2025/1/26 15:16
  • 上次更新2025/1/26 20:29:10
查看原帖
C++实参列表从右往左算
601284
__RelativitY__楼主2025/1/26 15:16

省流:C++逗号运算符左结合,但调用函数时计算顺序相反

做题 ing...(P1175)诶等下 #2 到 #10 全 WA 了?而且都显示 line 2 有问题

看题解,测了一组

(2+3)*(4+5)

第一行2 3 + 4 5 + *没问题嘛……等等怎么先算的 4+5?!

最后发现是这个:

tr[x]=node(s[p0],dfs(l,p0-1),dfs(p0+1,r));

这里会先调用右边的 dfs,导致后序遍历的“左-右-根”变成“右-左-根”(注:node(int,int,int)是结构体构造函数)

是这样吗?于是写了测试程序:

#include <bits/stdc++.h>
using namespace std;

int f(int x){
	cout<<"test"<<x<<endl;
	return x;
}

int add(int a,int b){
	return a+b;
}

int main(){
	f(1),f(2);
	cout<<"-----"<<endl;
	add(f(1),f(2));
	return 0;
}

输出是这样的:

test1
test2
-----
test2
test1

猜想成立,改完 AC,遂写此记。

2025/1/26 15:16
加载中...