fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main() {
  8. size_t n = 10;
  9.  
  10. // Best would be to use vectors. These are variable sized and dynamic
  11. vector<int> varray(n);
  12. cout<<varray.size()<<endl;
  13.  
  14. // But if you need raw arrays you can still go for:
  15. int *array = new int[n];
  16. for (int i=0; i<n; i++) // just filling some data
  17. varray[i]=array[i]=i*7;
  18.  
  19. // here the solution with raw array
  20. int last[5];
  21. copy(array+n-5, array+n, last); // array version
  22. copy(last, last+5, ostream_iterator<int>(cout," ")); // display result in one line
  23. cout<<endl;
  24.  
  25. // here the solution with vectors
  26. vector<int> vlast(5);
  27. copy(varray.end()-vlast.size(), varray.end(), vlast.begin());
  28.  
  29. copy(vlast.begin(), vlast.end(), ostream_iterator<int>(cout," ")); // display
  30. cout<<endl;
  31. return 0;
  32. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
10
35 42 49 56 63 
35 42 49 56 63