fork download
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. int main () {
  5. const std::size_t board_size = 16;
  6. char board [board_size] = {
  7. 0, 2, 0, 4,
  8. 2, 4, 8, 2,
  9. 0, 0, 8, 4,
  10. 2, 0, 0, 2
  11. };
  12.  
  13. // Initialize a zero-filled vector of the appropriate size.
  14. std::vector<int> zeroes(board_size);
  15.  
  16. // Fill the vector with index values (0 through board_size - 1).
  17. std::iota(zeroes.begin(), zeroes.end(), 0);
  18.  
  19. // Remove the index values that do not correspond to zero elements in the board.
  20. zeroes.erase(std::remove_if(zeroes.begin(), zeroes.end(), [&board] (int i) {
  21. return board[i] != 0;
  22. }), zeroes.end());
  23.  
  24. // Output the resulting contents of the vector.
  25. for (int i : zeroes) {
  26. std::cout << i << std::endl;
  27. }
  28. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
0
2
8
9
13
14