fork download
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. struct Foo
  5. {
  6. Foo(int i) : i(i) {}
  7. int i;
  8. };
  9.  
  10. int main(int,char**)
  11. {
  12. srand(42);
  13. Foo *src[10];
  14. for (int i = 0; i<10; i++)
  15. {
  16. src[i] = new Foo(i);
  17. }
  18.  
  19. Foo *dst[10];
  20. for (int i = 0; i<10; i++)
  21. {
  22. Foo *src_elem = NULL;
  23. while (src_elem == NULL)
  24. {
  25. int random_index = rand()%10;
  26. src_elem = src[random_index];
  27. src[random_index] = NULL;
  28. }
  29. dst[i] = src_elem;
  30. }
  31.  
  32.  
  33. for (int i = 0; i<10; i++)
  34. {
  35. std::cout << dst[i]->i << std::endl;
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
6
0
1
2
8
5
3
4
7
9