fork download
  1. #include "stdafx.h"
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class klasa {
  9. public:
  10. static int total;
  11.  
  12. int ilosc;
  13. klasa(int n) : ilosc(n) {
  14. std::cout << "created with " << ilosc << "\n";
  15. total += ilosc;
  16. };
  17. klasa(const klasa& drugi) {
  18. std::cout << "copy created with " << ilosc << "\n";
  19. ilosc = drugi.ilosc;
  20. total += ilosc;
  21. };
  22. klasa& operator=(const klasa& drugi) {
  23. std::cout << "assigned with " << ilosc << "\n";
  24. total -= ilosc;
  25. ilosc = drugi.ilosc;
  26. total += ilosc;
  27. return *this;
  28. };
  29. ~klasa() {
  30. std::cout << "deleted with " << ilosc << "\n";
  31. total -= ilosc;
  32. };
  33. };
  34.  
  35. int klasa::total = 0;
  36.  
  37. int main() {
  38. vector<klasa> vec;
  39. vec.reserve(310);
  40.  
  41. int suma = 0;
  42. for (int i = 100; i <= 500; i += 100) {
  43. suma += i;
  44. vec.emplace_back(i);
  45. }
  46.  
  47. cout << "Suma wrzuconych elementow: " << suma << " Rozmiar vectora: " << vec.size() << endl;
  48. cout << "klasa::total: " << klasa::total << endl;
  49.  
  50. cout << "before erase.. \n";
  51. for (const auto& x : vec)
  52. std::cout << x.ilosc << "\n";
  53.  
  54.  
  55. vec.erase(vec.begin(), vec.begin() + 2);
  56. cout << "after erase.. \n";
  57. for (const auto& x : vec)
  58. std::cout << x.ilosc << "\n";
  59.  
  60. cout << "vec.size() = " << vec.size() << endl;
  61. cout << "klasa::total = " << klasa::total << endl;
  62. return 0;
  63. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
created with 100
created with 200
created with 300
created with 400
created with 500
Suma wrzuconych elementow: 1500 Rozmiar vectora: 5
klasa::total: 1500
before erase.. 
100
200
300
400
500
assigned with 100
assigned with 200
assigned with 300
deleted with 400
deleted with 500
after erase.. 
300
400
500
vec.size() = 3
klasa::total = 1200
deleted with 300
deleted with 400
deleted with 500