{"id":152,"date":"2026-05-21T07:49:31","date_gmt":"2026-05-21T07:49:31","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=152"},"modified":"2026-05-24T15:39:10","modified_gmt":"2026-05-24T15:39:10","slug":"vectors","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=vectors","title":{"rendered":"Vectors"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Vectors in C++ are dynamic arrays provided by the Standard Template Library (STL). Unlike normal arrays, vectors can automatically grow or shrink in size during program execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Vector?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A vector is a sequence container that stores elements like an array but provides dynamic resizing and many useful built-in functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Vectors?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Vectors are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatically manage memory<\/li>\n\n\n\n<li>Resize dynamically<\/li>\n\n\n\n<li>Are easier to use than arrays<\/li>\n\n\n\n<li>Provide built-in STL functions<\/li>\n\n\n\n<li>Improve programming efficiency<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Vector Header File<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To use vectors in C++, include the <code>&lt;vector&gt;<\/code> library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;vector&gt;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Declaring a Vector<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>vector&lt;int&gt; numbers;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates an empty vector of integers.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Initializing a Vector<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>vector&lt;int&gt; numbers = {10, 20, 30, 40};<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Adding Elements Using push_back()<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><code>push_back()<\/code> adds elements at the end of the vector.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers.push_back(50);<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Accessing Vector Elements<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Elements can be accessed using indexing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>cout &lt;&lt; numbers&#91;0];<br>cout &lt;&lt; numbers.at(1);<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Example Program of 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; numbers = {1, 2, 3};<br><br>    numbers.push_back(4);<br><br>    for (int i = 0; i &lt; numbers.size(); i++) {<br><br>        cout &lt;&lt; numbers&#91;i] &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>1<br>2<br>3<br>4<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Common Vector 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>push_back()<\/code><\/td><td>Add element at end<\/td><\/tr><tr><td><code>pop_back()<\/code><\/td><td>Remove last element<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Get number of elements<\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>Remove all elements<\/td><\/tr><tr><td><code>empty()<\/code><\/td><td>Check if vector is empty<\/td><\/tr><tr><td><code>at()<\/code><\/td><td>Access element safely<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Example of Vector Functions<\/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; numbers = {10, 20, 30};<br><br>    numbers.pop_back();<br><br>    cout &lt;&lt; numbers.size();<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>2<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Using Range-Based Loop with Vector<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Modern C++ supports range-based loops.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\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; numbers = {5, 10, 15};<br><br>    for (int num : numbers) {<br><br>        cout &lt;&lt; num &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>5<br>10<br>15<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Vector Iterators<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Vectors support iterators for traversal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\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 = {1, 2, 3};<br><br>    for (auto 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<h1 class=\"wp-block-heading\">Difference Between Arrays and Vectors<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Arrays<\/th><th>Vectors<\/th><\/tr><\/thead><tbody><tr><td>Fixed size<\/td><td>Dynamic size<\/td><\/tr><tr><td>Manual memory handling<\/td><td>Automatic memory management<\/td><\/tr><tr><td>Limited functions<\/td><td>Many built-in functions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Important Points About Vectors<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Elements are stored in contiguous memory<\/li>\n\n\n\n<li>Vectors resize automatically<\/li>\n\n\n\n<li>Access is fast using indexing<\/li>\n\n\n\n<li>Inserting at end is efficient<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Vectors<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dynamic resizing<\/li>\n\n\n\n<li>Easy memory management<\/li>\n\n\n\n<li>STL support<\/li>\n\n\n\n<li>Flexible operations<\/li>\n\n\n\n<li>Better usability than arrays<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes with Vectors<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Accessing invalid index<\/li>\n\n\n\n<li>Forgetting vector size checks<\/li>\n\n\n\n<li>Using large unnecessary copies<\/li>\n\n\n\n<li>Confusing <code>size()<\/code> with capacity<\/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 shopping cart:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Items can be added anytime<\/li>\n\n\n\n<li>Items can be removed anytime<\/li>\n\n\n\n<li>Cart size changes dynamically<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is similar to how vectors work.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Applications of Vectors<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Vectors are widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data storage<\/li>\n\n\n\n<li>Game development<\/li>\n\n\n\n<li>Competitive programming<\/li>\n\n\n\n<li>Database applications<\/li>\n\n\n\n<li>Dynamic data processing<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Why Vectors are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Vectors are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplify array handling<\/li>\n\n\n\n<li>Improve memory management<\/li>\n\n\n\n<li>Increase programming efficiency<\/li>\n\n\n\n<li>Support dynamic applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Vectors in C++ are powerful dynamic containers that automatically manage memory and resize during runtime. With built-in STL functions and flexible operations, vectors are widely used in modern C++ programming for efficient and scalable applications.<\/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) > Vectors<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":52,"template":"","class_list":["post-152","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>Vectors - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn vectors in C++ with syntax, functions, iterators, dynamic resizing, and STL operations for efficient data handling and programming.\" \/>\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=\"Vectors - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn vectors in C++ with syntax, functions, iterators, dynamic resizing, and STL operations for efficient data handling and programming.\" \/>\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:39:10+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=vectors\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Vectors - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:49:31+00:00\",\"dateModified\":\"2026-05-24T15:39:10+00:00\",\"description\":\"Learn vectors in C++ with syntax, functions, iterators, dynamic resizing, and STL operations for efficient data handling and programming.\",\"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) > Vectors\"}]},{\"@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":"Vectors - Learn C++Language with GiGz.PK","description":"Learn vectors in C++ with syntax, functions, iterators, dynamic resizing, and STL operations for efficient data handling and programming.","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":"Vectors - Learn C++Language with GiGz.PK","og_description":"Learn vectors in C++ with syntax, functions, iterators, dynamic resizing, and STL operations for efficient data handling and programming.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:39:10+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=vectors","url":"https:\/\/gigz.pk\/","name":"Vectors - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:49:31+00:00","dateModified":"2026-05-24T15:39:10+00:00","description":"Learn vectors in C++ with syntax, functions, iterators, dynamic resizing, and STL operations for efficient data handling and programming.","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) > Vectors"}]},{"@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\/152","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=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}