fork download
  1. #include <algorithm> // std::reverse_copy()
  2. #include <cstdint> // std::int32_t
  3. #include <iostream>
  4. #include <iterator> // std::ostream_iterator
  5. #include <vector>
  6.  
  7. // you could use a typedef to save some typing
  8. // also to give more meaning to your vector
  9. typedef std::vector<int> SplitValues;
  10.  
  11. SplitValues splitter(std::int32_t number)
  12. {
  13. SplitValues values(8);
  14.  
  15. // work on vector elements using iterators
  16. for (auto& iter : values)
  17. {
  18. iter = static_cast<int>(number & 0xF); // cast the C++ way
  19. number >>= 4; // shorter form
  20. }
  21.  
  22. return values;
  23. }
  24.  
  25. int main()
  26. {
  27. std::int32_t number = 432214123;
  28.  
  29. // local vector assigned to one returned from function
  30. SplitValues values = splitter(number);
  31.  
  32. // display vector in reverse order using ostream iterator
  33. // this is done with one (wrapped) line and no loop
  34. std::reverse_copy(values.begin(), values.end(),
  35. std::ostream_iterator<int>(std::cout, " "));
  36. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1 9 12 3 1 0 6 11