Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class IntSet(){
  2.  
  3. IntSet::IntSet(std::size_t max_elem)
  4. {
  5. :_nm(0)
  6. , _cp(max_elem)
  7. , _data((int *)malloc(_cp * sizeof(int)))
  8. assert(_data)
  9. }
  10.  
  11. IntSet::~IntSet()
  12. {
  13. free(this->data)
  14. }
  15.  
  16. IntSet::IntSet(const IntSet &other)
  17. {
  18. _nm = other._nm; //:_nm(other._nm)
  19. _cp = other._cp; //,_cp(other._cp)
  20. _data = (int *)std::malloc(_cp * sizeof(int));
  21. assert(_data);
  22. std::memcpy(_data, other._data, sizeof(int) * _nm);
  23. }
  24.  
  25. IntSet::swap(IntSet &other)
  26. {
  27. using std::swap; //automatically destruct of copy (other), so free is not needed
  28. swap(_nm, _cp);
  29. }
  30.  
  31. &IntSet IntSet::operator=(IntSet other)
  32. {
  33. swap(other);
  34. return *this;
  35. }
  36.  
  37.  
  38. //want to realize "<<" as adding operator
  39.  
  40. &IntSet IntSet::operator<<(int elem)
  41. {
  42. assert(nm < _cp);
  43. _nm++;
  44. *(data + sizeof(int)) = elem;
  45. return *this;
  46. }
  47.  
  48. //want to check if the element with value elem is in array
  49.  
  50.  
  51. bool IntSet::operator[](int elem)
  52. {
  53. for (std::size_t i = 0; i < _nm; i++)
  54. {
  55. if (_data[i] == elem)
  56. {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62.  
  63.  
  64. //want to check if A belongs to B
  65.  
  66. bool IntSet::operator<=(const IntSet &other)
  67. {
  68. for (std::size_t i = 0; i < _nm; i++)
  69. {
  70. if (! other[_data[i]])
  71. {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77.  
  78.  
  79. //want to check if arrays are the same
  80.  
  81. bool IntSet::operator==(const IntSet &other)
  82. {
  83. return *this<=other && other<=*this;
  84. }
  85.  
  86. //want to
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement