what are templates in c++?

What are templates c++? where to start in learning templates,vectors,hashmap and sets in c++?

Please answer something understandable and maybe links of video tutorials on youtube.

Templates are a way of making your classes more abstract by letting you define the behavior of the class without actually knowing what datatype will be handled by the operations of the class.


You can read about the template here

Or you can see the topcoder tutorial of template here

You can also pre define your template so that you can save time in competitive programming. like this…

using namespace std;
#define mod 1000000007
typedef long long ll;
typedef pair < int, int > pii;
typedef vector<pii> vii;typedef vector<int> vi;
#define pi pair<int,int>
#define pb emplace_back
#define F first
#define S second
#define fills(x,y) memset(x,y,sizeof x)
#define rep(i,n) for(i=0;i<n;++i)
#define REP(i,k,n) for(i=k;i<=n;++i)
#define tr(i,c) for(auto &i:c)
#define fastio          ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define inchar          getchar
#define outchar(x)      putchar(x)
#define bitcount(n) __builtin_popcountll(n)
#define sz(a) int((a).size())
#define all(v) v.begin(),v.end()
//static const int inf = INT_MAX;static const long long infl = LONG_MAX;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
template<typename T> void scan(T &x){x=0;bool neg=0;register T c=inchar();if(c=='-')
template<typename T> void output(T n){bool neg=0;if(n<0)n*=-1,neg=1;char snum[65];int i=0;do {snum[i++]=n%10+'0';n/=10;}\
while(n);i=i-1; if(neg)putchar('-'); while(i>=0)outchar(snum[i--]);outchar('\n');}
inline void instr(char *str){register char c=0;register int i=0;while(c<33)c=inchar();while (c!='\n'&&c!=' '&&c!=EOF){\
template<typename T> T lcm(T a, T b){return (a*(b/__gcd(a,b)));}
template<typename T> T MOD(T a, T b) {return (a<b ? a : a%b);}
template<typename T> T add(T a, T b, T c){T x=a+b;return (x>=c ? x-c : x);}
template<typename T> T mod_neg(T a, T b) {a=MOD(a, b);if(a<0){a+=b;}return a;}
template<typename T> T expo(T e, T n){T x=1,p=e;while(n){if(n&1)x=x*p;p=p*p;n>>=1;}return x;}
template<typename T> T power(T e, T n, T m){T x=1,p=e;while(n){if(n&1)x=MOD(x*p,m);p=MOD(p*p,m);n>>=1;}return x;}
template<typename T> T extended_euclid(T a, T b, T &x, T &y){T xx=0,yy=1;y=0;x=1;while(b){T q=a/b,t=b;b=a%b;a=t;t=xx;\
xx=x-q*xx;x=t;t=yy;yy=y-q*yy;y=t;}return a;}
template<typename T> T mod_inverse(T a, T n){T x,y;T d = extended_euclid(a, n, x, y);return (d>1?-1:mod_neg(x,n));}
#define tcsolve() int tcs; scan(tcs); while(tcs--) solve();
inline string to_string(int num){stringstream ss;ss<<num;return ss.str();}
#define MAX 100002
string str;
int arr[MAX];
void solve()
int32_t main()
scan() // taking input
output()  //Printing output
tcsolve();  //Avoid typing again and again T(test case)

///****And much more***///

I hope these will be usefull for you! If you have still doubt then ask freely…

1 Like

This is a video tutorial on youtube for templates. This video is helpful if you are a beginner.

Here’s a playlist on youtube on basic c++ programming skills.

Tutorials point(templates)


I hope this may be helpful to you.

1 Like

Thanks @bansal1232 I am new in coding. Your answer is impressive but I can’t understand it. Can you suggest me where to start in learning templates,vectors,hashmap and sets in c++.