我很难理解下面的代码buffer[i] = arr ? arr[i] : 0;
。这是否意味着如果arr
包含任何东西,则它等于buffer[i]
,如果不包含,则等于0
?
#include <iostream>
using namespace std;
int main ()
{
int arr[5]={11,22,33,44,55};
int * buffer;
buffer = new int [5];
for(int i=0;i<5;i++){
buffer[i] = arr ? arr[i] : 0;//true/falls
cout<<buffer[i]<<",";
}
cout<<endl;
int arr2[5]={};
int * buffer2;
buffer2 = new int [5];
for(int i=0;i<5;i++){
buffer2[i] = arr2 ? arr2[i] : 0;//true/falls
cout<<buffer2[i]<<",";
}
cout<<endl;
}