fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <iterator>
  4.  
  5.  
  6. using std::cout; using std::endl;
  7. using std::ostream; using std::vector;
  8.  
  9.  
  10. class Integer {
  11.  
  12. public:
  13. Integer() {
  14. cout << "C " << this << " default" << endl;
  15. x = -1; // default value
  16. }
  17.  
  18. ~Integer() {
  19. cout << "D " << this << endl;
  20. x = -5; // destructed value. Not 0 so we can clearly see it
  21. }
  22.  
  23. Integer(int r) {
  24. cout << "C " << this << " value " << r << endl;
  25. x = r;
  26. }
  27.  
  28. Integer(const Integer& other) {
  29. cout << "C " << this << " copy from " << &other << endl;
  30. x = other.x;
  31. }
  32.  
  33. Integer operator=(const Integer& other) {
  34. cout << "A " << this << " copy from " << &other << endl;
  35. x = other.x;
  36. return *this;
  37. }
  38.  
  39. operator int() const{
  40. return x;
  41. }
  42.  
  43. friend ostream& operator<<(ostream& os, const Integer& thing) {
  44. os << thing.x;
  45. return os;
  46. }
  47.  
  48. private:
  49. int x;
  50. };
  51.  
  52.  
  53. ostream& operator<<(ostream& os, const vector<Integer> &v) {
  54. std::copy(v.begin(), v.end(), std::ostream_iterator<Integer>(os, ", "));
  55. return os;
  56. }
  57.  
  58. int main() {
  59. std::vector<Integer> ret {18, 7, 4, 24,11};
  60. cout << "Before: " << ret << endl;
  61. cout << " ret[0] is at " << &ret[0] << endl;
  62. ret.insert(ret.begin() + 1, ret[2]);
  63. cout << "After: " << ret << endl;
  64. cout << " ret[0] is at " << &ret[0] << endl;
  65. return 0;
  66. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
C 0x7ffd390c9a10 value 18
C 0x7ffd390c9a14 value 7
C 0x7ffd390c9a18 value 4
C 0x7ffd390c9a1c value 24
C 0x7ffd390c9a20 value 11
C 0x2add27a0ec30 copy from 0x7ffd390c9a10
C 0x2add27a0ec34 copy from 0x7ffd390c9a14
C 0x2add27a0ec38 copy from 0x7ffd390c9a18
C 0x2add27a0ec3c copy from 0x7ffd390c9a1c
C 0x2add27a0ec40 copy from 0x7ffd390c9a20
D 0x7ffd390c9a20
D 0x7ffd390c9a1c
D 0x7ffd390c9a18
D 0x7ffd390c9a14
D 0x7ffd390c9a10
Before: 18, 7, 4, 24, 11, 
  ret[0] is at 0x2add27a0ec30
C 0x2add27a0ec54 copy from 0x2add27a0ec38
C 0x2add27a0ec50 copy from 0x2add27a0ec30
C 0x2add27a0ec58 copy from 0x2add27a0ec34
C 0x2add27a0ec5c copy from 0x2add27a0ec38
C 0x2add27a0ec60 copy from 0x2add27a0ec3c
C 0x2add27a0ec64 copy from 0x2add27a0ec40
D 0x2add27a0ec30
D 0x2add27a0ec34
D 0x2add27a0ec38
D 0x2add27a0ec3c
D 0x2add27a0ec40
After: 18, 4, 7, 4, 24, 11, 
  ret[0] is at 0x2add27a0ec50
D 0x2add27a0ec50
D 0x2add27a0ec54
D 0x2add27a0ec58
D 0x2add27a0ec5c
D 0x2add27a0ec60
D 0x2add27a0ec64