fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class klasa{
  7. public:
  8. static int total;
  9.  
  10. int ilosc;
  11. klasa(int n) : ilosc(n) {total += ilosc;};
  12. //klasa(const klasa &drugi) { ilosc = drugi.ilosc; total += ilosc; };
  13. //klasa& operator=(const klasa &drugi) { ilosc = drugi.ilosc; total += ilosc; return *this; };
  14. ~klasa() { total -= ilosc;};
  15. };
  16.  
  17. int klasa::total = 0;
  18.  
  19. int main()
  20. {
  21. vector<klasa> vec;
  22.  
  23. int suma = 0;
  24. for(int i = 100; i <= 500; i+=100)
  25. {
  26. suma += i;
  27. klasa temp(i);
  28. vec.push_back(temp);
  29. }
  30.  
  31. cout << "Suma wrzuconych elementow: " << suma << " Rozmiar vectora: " << vec.size() << endl;
  32. for (const auto& x : vec)
  33. std::cout << x.ilosc << "\n";
  34.  
  35. cout << "before erase.. \n";
  36. vec.erase(vec.begin(), vec.begin()+2);
  37. cout << "after erase.. \n";
  38. for (const auto& x : vec)
  39. std::cout << x.ilosc << "\n";
  40.  
  41. cout << "vec.size() = " << vec.size() << endl;
  42. cout << "klasa::total = " << klasa::total << endl;
  43. return 0;
  44. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Suma wrzuconych elementow: 1500 Rozmiar vectora: 5
100
200
300
400
500
before erase.. 
after erase.. 
300
400
500
vec.size() = 3
klasa::total = -2300