Sunday, 22 September 2013

Using Templates in C++ Programming

I would describe about using templates in c++.

  1. functions using templates  
    #include <bits/stdc++.h>
    using namespace std;
    template <class T>
    T Max (T a, T b)
    {
    T res = a > b ? a : b;
    return res;
    }
    int main()
    {
    cout << Max <int> (3, 4) << endl; // 4
    cout << Max <double> (3.5, 4.6) << endl; // 4.6
    cout << Max <string> ("abc", "bcd") << endl; // bcd
    return 0;
    }

  2. classes using templates  
    #include <bits/stdc++.h>
    using namespace std;
    template <class T>
    class point
    {
    public :
    T x, y;
    point ()
    {
    }
    point (T _x, T _y)
    {
    x = _x;
    y = _y;
    }
    T dist ()
    {
    return x * x + y * y;
    }
    };
    int main()
    {
    point <int> p (3, 4);
    cout << p.dist() << endl; // 25
    point <double> q (3.5, 4.5);
    cout << q.dist() << endl; // 32.5
    return 0;
    }

  3. Template Specialization  
    Template Specialization means that specialize your template for some particular type. eg. you define add function for integers to be integers addition but for strings it may be addition. So You need to define it seperately.
    #include <bits/stdc++.h>
    using namespace std;
    template <class T>
    class SquareIt
    {
    public:
    T x;
    SquareIt ()
    {
    }
    SquareIt (T _x)
    {
    x = _x;
    }
    T getResult ()
    {
    return x * x;
    }
    };
    template <>
    class SquareIt <string>
    {
    public:
    string x;
    SquareIt ()
    {
    }
    SquareIt (string _x)
    {
    x = _x;
    }
    string getResult ()
    {
    return x + x;
    }
    };
    int main()
    {
    SquareIt <int> sq1 (5);
    cout << sq1.getResult() << endl;
    SquareIt <string> sq2 ("abc");
    cout << sq2.getResult() << endl;
    return 0;
    }

Tuesday, 17 September 2013

My Latest Template for Programming Contests


I have learnt few good things about c++.


  • you can directly use this header file, it will precompile all the header files required.

#include <bits/stdc++.h>
using namespace std;


  • struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _;

this is meant for reducing slowness of slowness of iostream in syncing. more on
http://codeforces.com/blog/entry/8387
http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_headers.html
http://codeforces.com/blog/entry/925

Precaution: You need to make sure that you do not use scanf and cin statements interchangibly means
one code should exclusively use cin and cout's only if you are using this. Reason is quite obvious.

I have tested the code on codechef. It works great :). Infact slightly faster :)

Here is the link of my latest template:

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) int((a).size())
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,a) FOR(i,0,(a)-1)
#define FORD(i,n,a) for(int (i)=(n);(i)>=a;(i)--)
#define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define FF first
#define SS second
#define FILL(a, v) memset(a, v, sizeof(a));
#define DREP(a) sort(ALL(a)); a.erase(unique(ALL(a)),a.end()) // will make the vector unique and sorted order
#define DEBUG(args...) {dbg,args; cerr<<endl;}
typedef long long LL;
typedef long double LD;
typedef vector <int> VI;
typedef vector <LL> VLL;
typedef vector <double> VD;
typedef vector<string> VS;
typedef vector <VI> VVI;
typedef pair <int,int> PII;
typedef pair <LL,LL> PLL;
typedef vector <PII > VPII;
struct debugger { template<typename T> debugger& operator , (const T& v) { cerr<<v<<" "; return *this; } } dbg;
template<class T> void print(vector < T > v, int sz=-1) {
if (sz == -1) sz = SZ(v);
cerr << "[";if (sz) cerr << v[0];FOR(i, 1, sz) cerr << ", " << v[i];cerr << "]" << endl;}
const double PI = acos(-1.0); //M_PI;
// main code starts now.
const int MAXN = (int) 1e5 + 10;
void read() {
}
void solve() {
}
int main() {
int T = 1;
//scanf ("%d", &T);
while (T--) {
read();
solve();
}
return 0;
}
view raw template.cpp hosted with ❤ by GitHub