C++中三元运算符的意外行为

emd*_*lam 4 c++ conditional-operator

以下是我编写的代码片段:

int n,i,j;
map<int,int>mp;
vector<int>vec;
cin>>n;
for(i=0; i<n; i++)
{
    cin>>j;
    mp[j]==0? mp[j]=1,vec.push_back(j): mp[j]=1;
}
Run Code Online (Sandbox Code Playgroud)

对于for循环内的第二行,CodeBlocks-16.01 版本显示以下错误:

second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
Run Code Online (Sandbox Code Playgroud)

但是当我将行更改为:

second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
Run Code Online (Sandbox Code Playgroud)

没有错误。以下行有什么问题?

mp[j]==0? vec.push_back(j), mp[j]=1: mp[j]=1;
Run Code Online (Sandbox Code Playgroud)

Kas*_*kar 7

要理解错误,让我们看一下条件运算符的操作数。

第二个操作数:

mp[j]=1, vec.push_back(j)
Run Code Online (Sandbox Code Playgroud)

操作数是由 分隔的两个表达式comma operator
逗号运算符在这里的工作方式是,它评估mp[j]=1哪个结果为 value 1,它丢弃该值并评估vec.push_back(j)返回的下一个表达式void

因此整个第二个操作数最终值是类型(这是错误所说的)。void

第三个操作数:

mp[j]=1
Run Code Online (Sandbox Code Playgroud)

此表达式的计算结果为1,其类型为int。(因此它不是voidor thrown-exception,这就是错误所说的)。

当您更改第二个操作数时:
在表达式中

vec.push_back(j), mp[j]=1
Run Code Online (Sandbox Code Playgroud)

vec.push_back(j)计算为void,此值将被丢弃,然后mp[j]=1计算1为 type int。现在两个操作数都是int,因此没有错误。