another interesting tid-bit

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

another interesting tid-bit

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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: another interesting tid-bit

Post by chili » December 3rd, 2017, 3:27 am

If you like that, you're gonna LOVE this:

https://ericniebler.github.io/range-v3/ ... uick-start

This is the future of containers/algorithms in C++.
Chili

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: another interesting tid-bit

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

Re: another interesting tid-bit

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

Post Reply