Lesson 22 of 24

Templates: Generic CodeTemplates: Generic Code

9 min 35 XP

Template மூலம் ஒரு function அல்லது class-ஐ எந்த type-க்கும் வேலை செய்யும்படி எழுதலாம். vector<int>, vector<string> எல்லாம் templates தான்!

A template lets one function or class work with any type. vector<int> and vector<string> are templates themselves!

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Challenge

+35 XP

template <typename T> class Stack எழுதுங்கள்: push, pop, top, empty (உள்ளே vector<T>). main()-ல் n எண்களை push செய்து, பிறகு காலியாகும் வரை pop செய்து print செய்யுங்கள்; பிறகு மூன்று சொற்களுக்கும் அதே. Input: 3 1 2 3 red green blue Output: 3 2 1 blue green red

Write template <typename T> class Stack with push, pop, top and empty, backed by a vector<T>. In main(), push n numbers and pop them all while printing, then do the same with three words. Input: 3 1 2 3 red green blue Output: 3 2 1 blue green red

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.