小编emd*_*lam的帖子

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

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

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)

c++ conditional-operator

4
推荐指数
1
解决办法
75
查看次数

标签 统计

c++ ×1

conditional-operator ×1