fork download
  1. #include <valarray>
  2. #include <cstdarg>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. std::valarray<std::size_t> selection(std::size_t num, ... )
  7. {
  8. va_list arguments;
  9. std::valarray<std::size_t> sel(num);
  10. va_start ( arguments, num );
  11. for ( std::size_t i = 0; i < num; i++ )
  12. sel[i] = va_arg ( arguments, int );
  13. va_end ( arguments );
  14. return sel;
  15.  
  16. }
  17.  
  18. int main ()
  19. {
  20. std::valarray<int> array(30);
  21. array[selection(3, 9,5,14)] = 1;
  22. array[selection(4, 8,15,13, 12)] = 2;
  23.  
  24. for (std::size_t i = 0, end = array.size(); i < end; ++i)
  25. std::cout << std::setw(2) << i << ',';
  26.  
  27. std::cout << '\n';
  28.  
  29. for (auto v : array)
  30. std::cout << std::setw(2) << v << ',';
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
 0, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 2, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,