{"id":162,"date":"2026-05-21T07:56:45","date_gmt":"2026-05-21T07:56:45","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=162"},"modified":"2026-05-24T15:57:08","modified_gmt":"2026-05-24T15:57:08","slug":"iterators","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=iterators","title":{"rendered":"Iterators"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Iterators in C++ are objects that are used to access and traverse elements of containers like vectors, lists, and maps in the Standard Template Library (STL).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Iterator?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An iterator works like a pointer. It points to elements inside a container and allows you to move through them one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Iterators?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Provide a common way to access STL containers<\/li>\n\n\n\n<li>Work with different data structures<\/li>\n\n\n\n<li>Replace indexing in containers like lists and maps<\/li>\n\n\n\n<li>Make code more flexible and reusable<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Basic Syntax<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>container_type::iterator it;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Iterator with Vector<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>using namespace std;<br><br>int main() {<br><br>    vector&lt;int&gt; v = {10, 20, 30};<br><br>    vector&lt;int&gt;::iterator it;<br><br>    for (it = v.begin(); it != v.end(); it++) {<br><br>        cout &lt;&lt; *it &lt;&lt; endl;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>10<br>20<br>30<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">How Iterators Work<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>begin()<\/code> points to the first element<\/li>\n\n\n\n<li><code>end()<\/code> points just after the last element<\/li>\n\n\n\n<li><code>*it<\/code> accesses the value<\/li>\n\n\n\n<li><code>it++<\/code> moves to next element<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Simplified Iterator Loop<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Modern C++ uses <code>auto<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (auto it = v.begin(); it != v.end(); it++) {<br><br>    cout &lt;&lt; *it &lt;&lt; endl;<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Iterator with List<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;list&gt;<br>using namespace std;<br><br>int main() {<br><br>    list&lt;int&gt; l = {1, 2, 3};<br><br>    for (auto it = l.begin(); it != l.end(); it++) {<br><br>        cout &lt;&lt; *it &lt;&lt; endl;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Iterator with Map<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;map&gt;<br>using namespace std;<br><br>int main() {<br><br>    map&lt;string, int&gt; m;<br><br>    m&#91;\"Ali\"] = 80;<br>    m&#91;\"Ahmed\"] = 90;<br><br>    for (auto it = m.begin(); it != m.end(); it++) {<br><br>        cout &lt;&lt; it-&gt;first &lt;&lt; \" : \" &lt;&lt; it-&gt;second &lt;&lt; endl;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali : 80<br>Ahmed : 90<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Types of Iterators<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Input Iterator<\/li>\n\n\n\n<li>Output Iterator<\/li>\n\n\n\n<li>Forward Iterator<\/li>\n\n\n\n<li>Bidirectional Iterator<\/li>\n\n\n\n<li>Random Access Iterator<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Important Iterator Operations<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>*it<\/code><\/td><td>Access value<\/td><\/tr><tr><td><code>it++<\/code><\/td><td>Move forward<\/td><\/tr><tr><td><code>it--<\/code><\/td><td>Move backward (some containers)<\/td><\/tr><tr><td><code>it-&gt;<\/code><\/td><td>Access object members<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Why Iterators are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Work with all STL containers<\/li>\n\n\n\n<li>Replace indexing in complex structures<\/li>\n\n\n\n<li>Make code generic and reusable<\/li>\n\n\n\n<li>Help in efficient traversal<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Difference Between Iterator and Pointer<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Iterator<\/th><th>Pointer<\/th><\/tr><\/thead><tbody><tr><td>Works with STL containers<\/td><td>Works with memory addresses<\/td><\/tr><tr><td>Safer and more flexible<\/td><td>Low-level access<\/td><\/tr><tr><td>Supports different containers<\/td><td>Works mainly with arrays<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Think of iterators like reading pages of a book:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start from first page<\/li>\n\n\n\n<li>Move page by page<\/li>\n\n\n\n<li>Stop at the end<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is how iterators move through data.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Applications of Iterators<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators are used in:<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779638231893\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data traversal in STL containers<\/li>\n\n\n\n<li>Algorithms like sorting and searching<\/li>\n\n\n\n<li>Complex data structure handling<\/li>\n\n\n\n<li>Generic programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators in C++ are powerful tools used to traverse and access elements in STL containers. They provide a standard and flexible way to work with different data structures, making code more efficient and reusable.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional C++ > STL (Standard Template Library) > Iterators<\/span><\/span><\/div>","protected":false},"menu_order":57,"template":"","class_list":["post-162","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Iterators - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ iterators in STL with syntax, usage in vector, list, map, traversal methods, and examples for efficient container access.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Iterators - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ iterators in STL with syntax, usage in vector, list, map, traversal methods, and examples for efficient container access.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-24T15:57:08+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=iterators\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Iterators - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:56:45+00:00\",\"dateModified\":\"2026-05-24T15:57:08+00:00\",\"description\":\"Learn C++ iterators in STL with syntax, usage in vector, list, map, traversal methods, and examples for efficient container access.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional C++ > STL (Standard Template Library) > Iterators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Iterators - Learn C++Language with GiGz.PK","description":"Learn C++ iterators in STL with syntax, usage in vector, list, map, traversal methods, and examples for efficient container access.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Iterators - Learn C++Language with GiGz.PK","og_description":"Learn C++ iterators in STL with syntax, usage in vector, list, map, traversal methods, and examples for efficient container access.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:57:08+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=iterators","url":"https:\/\/gigz.pk\/","name":"Iterators - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:56:45+00:00","dateModified":"2026-05-24T15:57:08+00:00","description":"Learn C++ iterators in STL with syntax, usage in vector, list, map, traversal methods, and examples for efficient container access.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Professional C++ > STL (Standard Template Library) > Iterators"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}