EWTD

iterator

May 4th, 2021
514
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×