{"id":160,"date":"2026-05-21T07:55:27","date_gmt":"2026-05-21T07:55:27","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=160"},"modified":"2026-05-24T15:54:09","modified_gmt":"2026-05-24T15:54:09","slug":"maps","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=maps","title":{"rendered":"Maps"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Maps in C++ are part of the Standard Template Library (STL). A map is a container that stores data in key-value pairs, where each key is unique.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Map?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A map is a data structure that stores elements in pairs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Key \u2192 Unique identifier<\/li>\n\n\n\n<li>Value \u2192 Data associated with the key<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Each key maps to exactly one value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Maps?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Maps are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Store data in an organized way<\/li>\n\n\n\n<li>Allow fast searching using keys<\/li>\n\n\n\n<li>Ensure unique keys<\/li>\n\n\n\n<li>Automatically sort data by keys<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Map Header File<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To use maps in C++, include the <code>&lt;map&gt;<\/code> library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;map&gt;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Declaring a Map<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>map&lt;string, int&gt; studentMarks;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Adding Elements<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">You can insert values using the key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>studentMarks&#91;\"Ali\"] = 85;<br>studentMarks&#91;\"Ahmed\"] = 90;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Accessing Elements<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>cout &lt;&lt; studentMarks&#91;\"Ali\"];<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Example Program of 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; marks;<br><br>    marks&#91;\"Ali\"] = 80;<br>    marks&#91;\"Sara\"] = 90;<br>    marks&#91;\"Ahmed\"] = 85;<br><br>    for (auto it : marks) {<br><br>        cout &lt;&lt; it.first &lt;&lt; \" : \" &lt;&lt; it.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>Ahmed : 85<br>Ali : 80<br>Sara : 90<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Common Map Functions<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>insert()<\/code><\/td><td>Add element<\/td><\/tr><tr><td><code>erase()<\/code><\/td><td>Remove element<\/td><\/tr><tr><td><code>find()<\/code><\/td><td>Search key<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Get number of elements<\/td><\/tr><tr><td><code>empty()<\/code><\/td><td>Check if map is empty<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Example of insert and find<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>marks.insert({\"Zain\", 75});<br><br>if (marks.find(\"Ali\") != marks.end()) {<br><br>    cout &lt;&lt; \"Found\";<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">How Map Works<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Stores data in key-value pairs<\/li>\n\n\n\n<li>Keys are always unique<\/li>\n\n\n\n<li>Data is automatically sorted by key<\/li>\n\n\n\n<li>Uses balanced tree structure internally<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">Types of Maps<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. Ordered Map<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stores data in sorted order<\/li>\n\n\n\n<li>Uses <code>map<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Unordered Map<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Faster access<\/li>\n\n\n\n<li>No sorting<\/li>\n\n\n\n<li>Uses <code>unordered_map<\/code><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Difference Between Map and Vector<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Map<\/th><th>Vector<\/th><\/tr><\/thead><tbody><tr><td>Key-value storage<\/td><td>Index-based storage<\/td><\/tr><tr><td>Unique keys<\/td><td>No key concept<\/td><\/tr><tr><td>Sorted by key<\/td><td>Not sorted automatically<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Important Points About Maps<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keys must be unique<\/li>\n\n\n\n<li>Values can repeat<\/li>\n\n\n\n<li>Automatically sorted by key<\/li>\n\n\n\n<li>Fast lookup using keys<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Maps<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Efficient searching<\/li>\n\n\n\n<li>Organized data storage<\/li>\n\n\n\n<li>Easy key-based access<\/li>\n\n\n\n<li>Useful for large datasets<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes with Maps<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using duplicate keys incorrectly<\/li>\n\n\n\n<li>Assuming insertion order is preserved<\/li>\n\n\n\n<li>Accessing non-existing keys without checking<\/li>\n\n\n\n<li>Confusing map with array indexing<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a dictionary:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Word = Key<\/li>\n\n\n\n<li>Meaning = Value<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You search by word to get meaning. This is how a map works.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Applications of Maps<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Maps are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Student record systems<\/li>\n\n\n\n<li>Phone directories<\/li>\n\n\n\n<li>Database indexing<\/li>\n\n\n\n<li>Caching systems<\/li>\n\n\n\n<li>Configuration settings<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Why Maps are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Maps are important because they:<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779638079502\"><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>Provide fast data retrieval<\/li>\n\n\n\n<li>Organize data efficiently<\/li>\n\n\n\n<li>Support real-world applications<\/li>\n\n\n\n<li>Improve program performance<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Maps in C++ are powerful STL containers that store data in key-value pairs. They provide fast searching, organized storage, and are widely used in real-world applications where efficient data access is required.<\/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) > Maps<\/span><\/span><\/div>","protected":false},"menu_order":56,"template":"","class_list":["post-160","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>Maps - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ maps in STL with key-value pairs, insertion, search, functions, and real-world data storage examples.\" \/>\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=\"Maps - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ maps in STL with key-value pairs, insertion, search, functions, and real-world data storage examples.\" \/>\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:54:09+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=maps\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Maps - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:55:27+00:00\",\"dateModified\":\"2026-05-24T15:54:09+00:00\",\"description\":\"Learn C++ maps in STL with key-value pairs, insertion, search, functions, and real-world data storage examples.\",\"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) > Maps\"}]},{\"@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":"Maps - Learn C++Language with GiGz.PK","description":"Learn C++ maps in STL with key-value pairs, insertion, search, functions, and real-world data storage examples.","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":"Maps - Learn C++Language with GiGz.PK","og_description":"Learn C++ maps in STL with key-value pairs, insertion, search, functions, and real-world data storage examples.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:54:09+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=maps","url":"https:\/\/gigz.pk\/","name":"Maps - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:55:27+00:00","dateModified":"2026-05-24T15:54:09+00:00","description":"Learn C++ maps in STL with key-value pairs, insertion, search, functions, and real-world data storage examples.","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) > Maps"}]},{"@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\/160","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=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}