Advertisement
yaffar

Untitled

Dec 18th, 2022
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | Source Code | 0 0
  1. #ifndef ARERASE__H
  2. #define ARERASE__H
  3.  
  4. #include <algorithm>
  5. #include <iostream>
  6.  
  7. template <class T>
  8. class array_eraser {
  9.  
  10.     T* t;
  11.     int sz;
  12.  
  13. public:
  14.  
  15.     typedef T* iterator;
  16.  
  17.     array_eraser( T* arr, int s )
  18.     {
  19.         t = arr;
  20.         sz = s;
  21.     }
  22.  
  23.     void erase( const T& e )
  24.     {
  25.         int i = 0;
  26.         while ( i < sz )
  27.         {
  28.             if ( t[ i ] == e ) { erase_index( i ); }
  29.             else { i++; }
  30.         }
  31.     }
  32.    
  33.     void erase_index( int ind ) {
  34.         T tmp = t[ ind ];
  35.         for ( int i = ind; i < sz - 1; i++ )
  36.         {
  37.             t[ i ] = t [ i + 1 ];
  38.         }
  39.         sz--;
  40.         t[ sz ] = tmp;
  41.     }
  42.  
  43.     int size() const { return sz; }
  44.    
  45.     int count( const T& e ) const
  46.     {
  47.         return std::count( begin(), end(), e );
  48.     }
  49.    
  50.     iterator begin() const {
  51.         return t;
  52.     }
  53.  
  54.     iterator end() const {
  55.         return t + sz;
  56.     }
  57.  
  58. };
  59.  
  60. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement