Doubt in iterator and template

What is actually the meaning of writing :

template<class T ? What is T representing here?

And what is the meaning of writing :

template<class ForwardIterator, class T> ? How forward Iterator class is different than class T ?

I am really unable to understand these terms. It seems to be more difficult than recursion. Please help !!!

hello @yashsharma4304

T is placeholder ie we can replace T with any dataype that we want to use.

T here is generic i.e we can use any datatype/class in place of T as per our need.
whereas ForwardIterator is a specific class we cant replace it with any other datatype.

pls read some articles on generic programing and c++ templates if u find it difficult.

1 Like

So in place of T can I write any other alphabet or is it some kind of fixed notation ?

U can use any other alphabet. . . . .
it will work fine.
example->

#include <iostream>
using namespace std;

// One function works for all data types. This would work
// even for user defined types if operator '>' is overloaded
template <typename F>
F myMax(F x, F y)
{
return (x > y)? x: y;
}

int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double
cout << myMax<char>('g', 'e') << endl; // call myMax for char

return 0;
}
1 Like

Ok thanks for resolving my doubt. I will read some articles to understand these topic better

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.