ideone

useful pieces of codes

View on GitHub

Problem

Create a quick direction iterator, which iterates over all 4 directions, i.e., up down left and right, in. a 2d space.

In many problems we have to do some operation on all the neighboring elements. So in that scenario we might have to write them one by one in the code. Instead of it if we generate the difference programatically we can write the code more neatly and understandable manner.

Solution

Code

https://ideone.com/CGUrP8

P.S.

8 directional trick: {0,-1,-1,0,1,-1,1,1,0}

Code

#include <iostream>
#include <vector>
using namespace std;
pair<int,int> dir(int d){
	d %= 4;
	return make_pair(d/2?d%2*2-1:0,d/2?0:d%2*2-1);
}
int main() {
	vector<string> v = {"left","right","up","down"};
	cout<<"i\tj"<<endl;
	for(int i=0; i<4; i++)
		cout<<dir(i).first<<'\t'<<dir(i).second<<"\td:"<<i<<'\t'<<v[i]<<endl;
	
	// second approach
	vector<int> dirs={0,1,0,-1,0};
	v = {"right","down","left","up"};
	cout<<endl<<"i\tj"<<endl;
	for(int i=0; i<4; i++)
		cout<<dirs[i]<<'\t'<<dirs[i+1]<<"\td:"<<i<<'\t'<<v[i]<<endl;
		
	return 0;
}

Ideone it!