{"id":17825,"date":"2025-04-02T09:00:00","date_gmt":"2025-04-02T04:00:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=17825"},"modified":"2025-04-02T09:00:00","modified_gmt":"2025-04-02T04:00:00","slug":"c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/","title":{"rendered":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that works for multiple data types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Templates enable generic programming, letting the compiler generate functions or classes for different data types automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Templates?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Template help you write code that can automatically adjust to different data types, making your programs cleaner, shorter, and more efficient.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A template in C++ is a generic blueprint that allows functions or classes\nto operate with different data types without rewriting the same code.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This introduces <code>T<\/code> as a placeholder type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Function Templates<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Function templates help avoid writing the same function multiple times for different data types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example (Traditional Approach):<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int maxInt(int a, int b);\ndouble maxDouble(double a, double b);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Function Template:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T&gt;\nT myMax(T a, T b) {\n    return (a &gt; b) ? a : b;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler automatically creates versions of this function based on the types used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Complete Example:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\ntemplate &lt;typename T&gt;\nT add(T a, T b) {\n    return a + b;\n}\n\nint main() {\n    cout &lt;&lt; add(3, 4) &lt;&lt; endl;\n    cout &lt;&lt; add(2.5, 3.1) &lt;&lt; endl;\n    cout &lt;&lt; add&lt;string&gt;(\"Hello \", \"World\") &lt;&lt; endl;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Class Templates<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Class templates generalize classes to store or operate on values of any type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Without Templates (Repetitive):<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class IntBox { int value; };\nclass FloatBox { float value; };\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Class Template:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T&gt;\nclass Box {\nprivate:\n    T item;\npublic:\n    void set(T value) { item = value; }\n    T get() const { return item; }\n};\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Box&lt;int&gt; b1;\nb1.set(100);\n\nBox&lt;string&gt; b2;\nb2.set(\"Apples\");\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Class Template Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\ntemplate &lt;class T&gt;\nclass Calculator {\nprivate:\n    T num1, num2;\npublic:\n    Calculator(T a, T b) : num1(a), num2(b) {}\n    T add() { return num1 + num2; }\n    T subtract() { return num1 - num2; }\n};\n\nint main() {\n    Calculator&lt;int&gt; obj1(10, 5);\n    cout &lt;&lt; obj1.add() &lt;&lt; endl;\n\n    Calculator&lt;int&gt; obj2(10.5, 2.2);\n    cout &lt;&lt; obj2.add() &lt;&lt; endl;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Why Templates Matter<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reusable code<\/strong>: Write once, use for any type.<\/li>\n\n\n\n<li><strong>Type safety<\/strong>: Compiler handles type correctness.<\/li>\n\n\n\n<li><strong>Performance<\/strong>: Optimized code generated per type.<\/li>\n\n\n\n<li><strong>Foundation of STL<\/strong>: The Standard Template Library uses templates extensively.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to STL (Standard Template Library)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To manage data efficiently in real-world applications, programmers need ready-made structures such as lists, maps, stacks, and sorting functions. Writing these from scratch every time is time\u2011consuming and error\u2011prone. The <strong>Standard Template Library (STL)<\/strong> provides these tools in a reusable and optimized form.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the student\u2011friendly definition:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>STL (Standard Template Library) is a collection of template-based data structures,\nalgorithms, and iterators that allow efficient storage, processing, and traversal\nof data in C++.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">STL provides the following major components:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Containers:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>vector&lt;T><\/code><\/li>\n\n\n\n<li><code>list&lt;T><\/code><\/li>\n\n\n\n<li><code>queue&lt;T><\/code><\/li>\n\n\n\n<li><code>stack&lt;T><\/code><\/li>\n\n\n\n<li><code>map&lt;Key,Value><\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Algorithms:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sort()<\/code><\/li>\n\n\n\n<li><code>find()<\/code><\/li>\n\n\n\n<li><code>count()<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Iterators:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Generic pointers for containers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">STL Example: vector<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main() {\n    vector&lt;int&gt; numbers;\n    numbers.push_back(10);\n    numbers.push_back(20);\n    numbers.push_back(30);\n\n    for (int x : numbers)\n        cout &lt;&lt; x &lt;&lt; \" \";\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">STL Example: pair&lt;T1,T2&gt;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;utility&gt;\nusing namespace std;\n\nint main() {\n    pair&lt;string, int&gt; student;\n    student.first = \"Ali\";\n    student.second = 22;\n    cout &lt;&lt; student.first &lt;&lt; \" is \" &lt;&lt; student.second &lt;&lt; \" years old.\";\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">STL Algorithm Example: sort()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\n\nint main() {\n    vector&lt;int&gt; data = {4, 1, 9, 2};\n    sort(data.begin(), data.end());\n\n    for (int x : data)\n        cout &lt;&lt; x &lt;&lt; \" \";\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Questions<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a function template <code>minValue()<\/code>.<\/li>\n\n\n\n<li>Create a class template <code>Storage&lt;T><\/code>.<\/li>\n\n\n\n<li>Create and print a vector of strings.<\/li>\n\n\n\n<li>Sort a vector of doubles.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that works for multiple data types. Templates enable generic programming, letting the compiler generate functions or classes for different data types automatically. What Are Templates? Template&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":17857,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[637],"tags":[733,730],"class_list":["post-17825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-standard-template-library","tag-template"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Afzal Badshah, PhD\"\/>\n\t<meta name=\"google-site-verification\" content=\"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI\" \/>\n\t<meta name=\"p:domain_verify\" content=\"87b5ca26c36438b20581a102dc290a47\" \/>\n\t<meta name=\"yandex-verification\" content=\"0eb3e2a9157fd34d\" \/>\n\t<meta name=\"keywords\" content=\"standard template library,template,oop with c++\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_GB\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2025-04-02T04:00:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-04-02T04:00:00+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/web.facebook.com\/abmanduri\/\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:title\" content=\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg\" \/>\n\t\t<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t\t<meta name=\"twitter:data1\" content=\"Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#blogposting\",\"name\":\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD\",\"headline\":\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial\",\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/OOP-6.jpg?fit=1920%2C1080&ssl=1\",\"width\":1920,\"height\":1080},\"datePublished\":\"2025-04-02T09:00:00+05:00\",\"dateModified\":\"2025-04-02T09:00:00+05:00\",\"inLanguage\":\"en-GB\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#webpage\"},\"articleSection\":\"OOP with C++, standard template library, Template\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/afzalbadshah.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"position\":2,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/#listItem\",\"name\":\"OOP with C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/#listItem\",\"position\":3,\"name\":\"OOP with C++\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#listItem\",\"name\":\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#listItem\",\"position\":4,\"name\":\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/#listItem\",\"name\":\"OOP with C++\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#personImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#authorImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/\",\"name\":\"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD\",\"description\":\"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2025\\\/11\\\/OOP-6.jpg?fit=1920%2C1080&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#mainImage\",\"width\":1920,\"height\":1080},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/04\\\/02\\\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\\\/#mainImage\"},\"datePublished\":\"2025-04-02T09:00:00+05:00\",\"dateModified\":\"2025-04-02T09:00:00+05:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"alternateName\":\"Afzal Badshah\",\"description\":\"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\",\"inLanguage\":\"en-GB\",\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD","description":"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/","robots":"max-image-preview:large","keywords":"standard template library,template,oop with c++","webmasterTools":{"google-site-verification":"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI","p:domain_verify":"87b5ca26c36438b20581a102dc290a47","yandex-verification":"0eb3e2a9157fd34d","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#blogposting","name":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD","headline":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial","author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg?fit=1920%2C1080&ssl=1","width":1920,"height":1080},"datePublished":"2025-04-02T09:00:00+05:00","dateModified":"2025-04-02T09:00:00+05:00","inLanguage":"en-GB","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#webpage"},"articleSection":"OOP with C++, standard template library, Template"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","position":1,"name":"Home","item":"https:\/\/afzalbadshah.com","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","position":2,"name":"Courses","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/#listItem","name":"OOP with C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/#listItem","position":3,"name":"OOP with C++","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#listItem","name":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#listItem","position":4,"name":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial","previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/#listItem","name":"OOP with C++"}}]},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/#person","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#personImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author","url":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#authorImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"WebPage","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/","name":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD","description":"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#breadcrumblist"},"author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"creator":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg?fit=1920%2C1080&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#mainImage","width":1920,"height":1080},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/#mainImage"},"datePublished":"2025-04-02T09:00:00+05:00","dateModified":"2025-04-02T09:00:00+05:00"},{"@type":"WebSite","@id":"https:\/\/afzalbadshah.com\/#website","url":"https:\/\/afzalbadshah.com\/","name":"Afzal Badshah, PhD","alternateName":"Afzal Badshah","description":"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","inLanguage":"en-GB","publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"}}]},"og:locale":"en_GB","og:site_name":"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","og:type":"article","og:title":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD","og:description":"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that","og:url":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg","og:image:width":1920,"og:image:height":1080,"article:published_time":"2025-04-02T04:00:00+00:00","article:modified_time":"2025-04-02T04:00:00+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial - Afzal Badshah, PhD","twitter:description":"Imagine running a grocery shop. You use different delivery boxes for apples, oranges, and eggs etc. But using a separate box for every item leads to complexity. Instead, one general-purpose box that can deliver any item is more efficient. Templates in C++ serve the same purpose: they allow writing a single piece of code that","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"3 minutes"},"aioseo_meta_data":{"post_id":"17825","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":true,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":{"faqs":[],"keyPoints":[],"titles":[],"descriptions":[],"socialPosts":{"email":[],"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2025-11-24 05:49:52","updated":"2025-11-24 06:08:59","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/\" title=\"Courses\">Courses<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/\" title=\"OOP with C++\">OOP with C++<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tC++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/afzalbadshah.com"},{"label":"Courses","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/"},{"label":"OOP with C++","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/"},{"label":"C++ Templates and Standard Template Library (STL): A Complete Beginner-Friendly Tutorial","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/04\/02\/c-templates-and-standard-template-library-stl-a-complete-beginner-friendly-tutorial\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/11\/OOP-6.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-4Dv","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/17825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=17825"}],"version-history":[{"count":1,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/17825\/revisions"}],"predecessor-version":[{"id":17859,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/17825\/revisions\/17859"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/17857"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=17825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=17825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=17825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}