+ 1

In which situation template<class T> is use?

31st Jan 2020, 3:31 AM
Shekhar Sharma
Shekhar Sharma - avatar
3 Answers
+ 10
We can use this for any data type that supports. // comparison operator < and swap works for it. template < class T>.
31st Jan 2020, 3:48 AM
SHADOW 🌀
SHADOW 🌀 - avatar
+ 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
31st Jan 2020, 3:37 AM
Avinesh
Avinesh - avatar
+ 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
31st Jan 2020, 3:39 AM
Arsenic
Arsenic - avatar