fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class A
  7. {
  8. public:
  9. A(const char *str)
  10. {
  11. cptr = new char [strlen(str)];
  12. strcpy(cptr,str);
  13. cout<<"A(const char *str) called and cptr is "<<cptr<<endl;
  14. }
  15. A(const A &a)
  16. {
  17. cptr = new char [strlen(a.cptr)+1];
  18. strcpy(this->cptr,a.cptr);
  19. cout<<"A(const A &a) called and cptr is "<<cptr<<endl;
  20. }
  21. ~A()
  22. {
  23. cout<<"~A() called and cptr is "<<cptr<<endl;
  24. delete [] cptr;
  25. }
  26.  
  27. char *cptr;
  28. };
  29.  
  30. int main()
  31. {
  32. A a1("1"),a2("2"),a3("3");
  33. vector<A> Avec;
  34. Avec.reserve(256);
  35.  
  36. Avec.push_back(a1);
  37. Avec.push_back(a2);
  38. Avec.push_back(a3);
  39.  
  40. vector<A>::iterator iter;
  41. for(iter = Avec.begin(); iter!=Avec.end(); )
  42. {
  43. if(strcmp((*iter).cptr,"1") == 0)
  44. {
  45. cout<<"before erase"<<endl;
  46. iter = Avec.erase(iter);
  47. cout<<"after erase"<<endl;
  48. }
  49. else
  50. {
  51. ++iter;
  52. }
  53. }
  54. }
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
A(const char *str) called and cptr is 1
A(const char *str) called and cptr is 2
A(const char *str) called and cptr is 3
A(const A &a) called and cptr is 1
A(const A &a) called and cptr is 2
A(const A &a) called and cptr is 3
before erase
~A() called and cptr is 3
after erase
~A() called and cptr is 2
~A() called and cptr is 
~A() called and cptr is 3
~A() called and cptr is 2
~A() called and cptr is 1