seems you forget about copy ctors.

You are right, the behaviour of vector is implementation defined. That's the gcc 3.4.2 output:

ctor 0
copy ctor 1
copy ctor 2
copy ctor 3
dtor 4
ctor 5
copy opt 6
dtor 7
dtor 8
dtor 9
dtor 10
Qt Code:
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. class Test
  5. {
  6. static int _no;
  7. public:
  8. Test()
  9. {
  10. std::cout << "ctor " << _no++ << std::endl;
  11. }
  12. Test(const Test&)
  13. {
  14. std::cout << "copy ctor " << _no++ << std::endl;
  15. }
  16. ~Test()
  17. {
  18. std::cout << "dtor " << _no++ << std::endl;
  19. }
  20. Test& operator= (const Test&)
  21. {
  22. std::cout << "copy opt " << _no++ << std::endl;
  23. }
  24. };
  25.  
  26. int Test::_no = 0;
  27.  
  28. int main()
  29. {
  30. std::vector<Test> x;
  31. x.resize(3);
  32.  
  33. x[ 15 ] = Test();
  34. return 0;
  35. }
To copy to clipboard, switch view to plain text mode