fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. struct Fred
  5. {
  6. int step;
  7. int ballChange;
  8. };
  9.  
  10. std::ostream& operator<< ( std::ostream& stm, const Fred& f )
  11. { return stm << "Fred{" << f.step << ',' << f.ballChange << '}' ; }
  12.  
  13. int main()
  14. {
  15. constexpr int N = 5 ;
  16.  
  17. {
  18. // value initialization
  19. std::unique_ptr< Fred[] > p( new Fred[N] () ) ;
  20. for( int i = 0 ; i < N ; ++i ) std::cout << p[i] << ' ' ;
  21. std::cout << '\n' ;
  22. }
  23.  
  24. {
  25. // addregate initialization
  26. std::unique_ptr< Fred[] > p( new Fred[N] { {0,1}, {2,3}, {4,5} } ) ;
  27. for( int i = 0 ; i < N ; ++i ) std::cout << p[i] << ' ' ;
  28. std::cout << '\n' ;
  29. }
  30.  
  31. // etc.
  32. }
  33.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Fred{0,0} Fred{0,0} Fred{0,0} Fred{0,0} Fred{0,0} 
Fred{0,1} Fred{2,3} Fred{4,5} Fred{0,0} Fred{0,0}