I am trying to implement a custom iterator for a simple class. This class is a pretty poor abstract for a fixed array (unfortunately, I cannot change it). Access to elements is possible only with an index.
template <class T>
struct data
{
static const size_t MAX_BUFFER{ 50 };
T* buffer_[MAX_BUFFER] = {};
int currpos_ = 0;
void insert(T *value) {
if (currpos_ < MAX_BUFFER-1)
buffer_[currpos_++] = value;
}
T** at(int i) {
if (i >= currpos_)
return NULL;
return &buffer_[i];
}
~data() {
for (int i=0; i<currpos_; ++i)
delete buffer_[i];
}
int entries() const { return currpos_; }
struct iterator : std::iterator<std::forward_iterator_tag, T*>
{
using reference = typename std::iterator<std::forward_iterator_tag, T*>::reference;
using pointer = typename std::iterator<std::forward_iterator_tag, T*>::pointer;
iterator(data<T> *d, int start) : p{ d }, index{ start } {}
iterator operator++() { if (index < p->entries()) ++index; return *this; }
friend bool operator==(const iterator &d1, const iterator &d2) { return d1.p == d2.p && d1.index == d2.index; }
friend bool operator!=(const iterator &d1, const iterator &d2) { return !(d1== d2); }
reference operator*() { return *(p->at(index)); }
pointer operator->() { return p->at(index); }
data<T> *p;
int index;
};
iterator begin() { return iterator(this, 0); }
iterator end() { return iterator(this, entries()); }
};
The problem I am facing is that with this interface I can use most standard STL algorithms such as for_each, transform, find_if. For example, suppose d is already initialized with new {2}, new {3}, new {4}, new {14}, new {-4}, new {-44}, new {42} this code
for (auto &i : d) std::cout <<*i <<" "; std::cout <<std::endl;
auto res=std::find_if(d.begin(), d.end(), [](auto &i) { return *i == -44;});
if (res != d.end())
std::cout <<**res <<std::endl;
std::transform(d.begin(), d.end(), d.begin(), [](auto &i) {*i *= 2; return i;});
for (auto &i : d) std::cout <<*i <<" "; std::cout <<std::endl;
will show correctly
2 3 4 14 -4 -44 42
-44
4 6 8 28 -8 -88 84
The problem I ran into is related to the std :: remove_if () algorithm and how it arranges the elements. I added a member function similar to vector :: erase:
void remove_range(iterator begin, iterator end)
{
size_t d=std::distance(begin, end);
currpos_ -= d;
}
, , , . , :
auto new_end = std::remove_if(d.begin(), d.end(), [](auto &r) { return *r > 3; });
d.remove_range(new_end, d.end());
valdring , 3 ( : 3 a >= 3). remove_range(), ( ). d:
(gdb) p d
$1 = {static MAX_BUFFER = <optimized out>, buffer_ = {0x603010, 0x603030, 0x603050, 0x603070, 0x603090, 0x6030b0, 0x6030d0, 0x0 <repeats 43 times>}, currpos_ = 7}
(gdb) p d
$2 = {static MAX_BUFFER = <optimized out>, buffer_ = {0x603010, 0x603030, 0x603090, 0x6030b0, 0x603090, 0x6030b0, 0x6030d0, 0x0 <repeats 43 times>}, currpos_ = 7}
, remove_if() ( > 3), , , , , , , , .
: ? - ?