Advertisement
EWTD

iterator

May 4th, 2021
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #pragma once
  2. #include <string>
  3. #include <set>
  4. #include "utils.h"
  5. inline bool is_vowel(const char c) noexcept{return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';}
  6. class IterableString{
  7.     const std::string _data;
  8.     class StringIterator{
  9.         const char* const _end;
  10.         const char* _data;
  11.     public:
  12.         StringIterator(const char* begin, const char* end)noexcept: _end(end){_data = begin;}
  13.         friend const StringIterator& operator++(StringIterator& s_it) noexcept{
  14.             s_it._data++;
  15.             while(s_it._data != s_it._end && !is_vowel(*s_it._data)){s_it._data++;}
  16.             return s_it;
  17.         }
  18.         friend const StringIterator operator++(StringIterator& s_it, int) noexcept{
  19.             auto res = s_it;
  20.             s_it._data++;
  21.             while(s_it._data != s_it._end && !is_vowel(*s_it._data)){s_it._data++;}
  22.             return res;
  23.         }
  24.         inline bool operator!=(const StringIterator& s_it)const noexcept{return _data != s_it._data;}
  25.         inline char operator*() const noexcept{return *_data;}
  26.     };
  27. public:
  28.     IterableString() = default;
  29.     explicit IterableString(std::string str): _data(move(str)){}
  30.     [[nodiscard]] inline auto begin() const noexcept{return StringIterator(_data.data(),_data.data()+_data.size());}
  31.     [[nodiscard]] inline auto end() const noexcept{return StringIterator(_data.data()+_data.size(),_data.data()+_data.size());}
  32. };
  33.  
  34. static void IterableStringTest(){
  35.     LOG("IterableStringTest")
  36.     IterableString a("agbgbgbgbgbb gb gb gbgb gbgbegbgbgbbgb b gbgbg gbgbgbgb gbgb gb bgbgbgbi");
  37.     std::string expected("aei");
  38.     std::string result;
  39.     for(char c: a){result.push_back(c);}
  40.     ASSERT(expected == result)
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement