TopCoderから学ぶ美しいマクロや型宣言 C++

TopCoderというプログラミングコンテスト
他人のコードから発見した、美しいマクロや
型宣言を紹介します。


これを導入することで、C++のコードが短くなり、
早くコーディングすることができます。


※すべてのTopCoder参加者がマクロなどをテンプレートと
して用意しているわけではありません。
マクロなどを定義している人は半分より少ないようです。


TopCoderの他人のコードを参考に、
マクロやtypedefによる型宣言をまとめました。
コードの全体はこのようになっています。
(あとで個別にコメントします)

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;


まずはinclude関係です。
TopCoderではよく使うインクルードファイルの宣言を
テンプレートとして用意している人が多いです。
TopCoder以外でも便利そうです。
特に便利なSTL(Standard Template Library)は、
vectorやlistを個別にインクルードする必要が
あるので、まとめておくと便利そうです。

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;


int型をstring型に変換する関数と、
string型をint型に変換する関数です。

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}


数学関数です。

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}


良く使う型宣言を短く言い換えています。
短く書くことで、コード全体が読みやすくなります。
便利で良く利用されるSTLの型がtypedefされています。

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;


STLのコンテナを短く扱いやすくするためのマクロです。
defineを使っているので、全て大文字で書いています。
これはマクロであるとわかりやすくするためです。

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())


おなじみのforループも短く言い換えます。

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)


よく使う定数です。

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);


メモリークリア。私は使い道がまだわかりませんが、
TopCoder参加者はよく使っています。

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))


デバッグ用のマクロです。
これが1番のオススメです。
TopCoderではなかなか見かけないマクロですが、
これはTopCoderに関係ない人にもとても便利だと
思うので紹介しました。
cerrの部分はcoutでも動くはずです。
printfデバッグとよく呼ばれる、
変数の値チェックの文を短く書けてとても楽です。

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

このデバッグ用マクロを実際に使うときは
セミコロンが必要ないようにしています。
デバッグ用だとわかりやすいと思ってこのようにしています。


今回紹介したものは、多人数での開発では勧められるものでは
ないかもしれません。ただ、うまく使えれば、コードが短くなり、
内容を把握しやすくなり、得意気にもなれます。
よろしければ、お試しください。