fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstddef>
  4.  
  5. class bitArray
  6. {
  7. private:
  8. std::vector<int> sortie;
  9. public:
  10. explicit bitArray(int size): sortie(size) {}
  11. bitArray& operator+=(const std::size_t i)
  12. {
  13. if (0 <= i && i < sortie.size()) // check for (0 <= index < size) of the array
  14. {
  15. this ->sortie[i] = 1;
  16. return *this;
  17. }
  18. else
  19. {
  20. // do your logic;
  21. // for instance, I have done something like follows:
  22. std::cout << "out of bound" << std::endl;
  23. if(sortie.size() == 0) // if the size of array == 0
  24. sortie.resize(1,0);
  25. }
  26. return *this;
  27. }
  28. int operator[] (const std::size_t index)
  29. {
  30. return (0 <= index && index < sortie.size()) ? // check for (0 <= index < size) of the array
  31. sortie[index] : -1;
  32. }
  33. };
  34. int main ()
  35. {
  36. bitArray obj(3);
  37. obj += 2;
  38. std::cout << obj[2] << std::endl;
  39.  
  40. obj += -2;
  41. std::cout << obj[-2] << std::endl;
  42.  
  43. obj += 22;
  44. std::cout << obj[22] << std::endl;
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 4484KB
stdin
Standard input is empty
stdout
1
out of bound
-1
out of bound
-1