Lesson 22 of 24

Templates: Generic Code

9 min 35 XP

Template से एक function या class किसी भी type के साथ काम कर सकती है। vector<int> और vector<string> खुद templates हैं!

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

vector<T> पर आधारित push, pop, top और empty वाली template <typename T> class Stack लिखें। main() में n संख्याएँ push करें और print करते हुए सब pop करें, फिर तीन शब्दों के साथ यही करें। 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.