The Partridge Family were neither partridges nor a family. Discuss.
-
MrGodin
- Posts: 721
- Joined: November 30th, 2013, 7:40 pm
- Location: Merville, British Columbia Canada
Post
by MrGodin » December 2nd, 2017, 7:40 pm
Range based iteration ?. There is this little thing i found interesting
Code: Select all
class Iterate
{
int min, max, pos;
Iterate* iter = nullptr;
Iterate(Iterate* iterator,int pos)
{
this->pos = pos;
this->iter = iterator;
}
public:
Iterate(int min, int max)
:min(min), max(max), pos(min)
{}
Iterate begin() { return Iterate(this, min); }
Iterate end() { return Iterate(this, max); }
// increment position
Iterate& operator++()
{
pos++;
return *this;
}
// use while iter != end()
bool operator != (Iterate& rhs)
{
return pos != rhs.pos;
}
// pointer overload
int operator* ()
{
return pos;
}
};
example
Code: Select all
int ints[4] = {1,2,3,4};
// iterates from 0 - 3
//0 = start index, 4 = end index
for(std::size_t i : Iterate(0,4))
{
ints[i]++;
}
Last edited by
MrGodin on December 7th, 2017, 2:02 am, edited 1 time in total.
Curiosity killed the cat, satisfaction brought him back
-
MrGodin
- Posts: 721
- Joined: November 30th, 2013, 7:40 pm
- Location: Merville, British Columbia Canada
Post
by MrGodin » December 3rd, 2017, 8:44 pm
That's an interesting read. A lot of c++ std stuff I've never used lol
Curiosity killed the cat, satisfaction brought him back
-
MrGodin
- Posts: 721
- Joined: November 30th, 2013, 7:40 pm
- Location: Merville, British Columbia Canada
Post
by MrGodin » December 3rd, 2017, 10:02 pm
I was trying to make that class a template but .... I'll figure it out another day lol.
Curiosity killed the cat, satisfaction brought him back