+ 1
In which situation template<class T> is use?
3 Réponses
+ 10
We can use this for any data type that supports. // comparison operator < and swap works for it. template < class T>.
+ 4
Templates in general were introduced to bring in generic concept. This way you can use different data types without rewriting the code for every type again.
This is an example-
You can apply this to classes as well.
https://code.sololearn.com/cHgvG8f3xY1E/?ref=app
+ 3
It saves us from writing same function for different data types again and again. For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter