arrays - I need help compressing this IF statment down as much as possible in C++ -


i have several if statements compress as possible each time in array number 1 appears add 1 ones int.

if (dice[1] == 1) {     ones ++; }  if (dice[2] == 1) {     ones ++; }  if (dice[3] == 1) {     ones ++; }  if (dice[4] == 1) {     ones ++; }  if (dice[5] == 1) {     ones ++; } 

you're counting 1 values in sequence , there's standard library algorithm that. takes 2 iterators , i'm giving pointers in case.

ones += std::count(&dice[1], &dice[6], 1); 

this same thing , people might prefer form.

ones += std::count(dice + 1, dice + 6, 1); 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -