{"id":28580,"date":"2023-05-30T11:59:55","date_gmt":"2023-05-30T06:29:55","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/"},"modified":"2024-09-03T11:08:23","modified_gmt":"2024-09-03T05:38:23","slug":"vectors-in-c","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/","title":{"rendered":"Vectors in C++ STL"},"content":{"rendered":"\n<p>When it comes to programming in C++, vectors are an essential data structure that every developer should be familiar with. Vectors provide a dynamic array-like structure that can store and manipulate elements efficiently. Whether you are a beginner or an experienced programmer, understanding vectors in C++ is crucial for building robust and flexible applications.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy PRO<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-title-link\">C Programming Course: Master C with Hands-on Projects<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Join our C Programming Course and learn C syntax, operators, expressions, control flow, functions, pointers, structures, file handling, memory management, and modular programming. Build real-world skills through practical projects!<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>2 Projects<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>10 Hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-button\">\n                C Programming Course with Certificate\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p>In this blog, we will explore the concept of vectors in C++ and delve into their various functionalities, including how to create and initialize vectors, access and modify elements, perform common operations, and utilize some advanced features. By the end of this blog, you will have a solid understanding of vectors and be equipped with the knowledge to leverage their power in your own C++ programs.<\/p>\n\n\n\n<p>STL stands for <a href=\"https:\/\/www.mygreatlearning.com\/blog\/standard-template-library-in-c\/\" target=\"_blank\" rel=\"noreferrer noopener\">Standard Template Library<\/a>. STL is a set of general-purpose classes and functions mainly for storing and processing data. STL can be defined as a library of container classes, algorithms, and iterators, and vectors in C++ is part of STL. The main idea behind STL is to reuse codes already written and tested. It saves time and effort.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"stl-has-four-components\"><strong>STL has four components<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Algorithms<\/strong>: It defines a collection of functions specially designed to be used on ranges of elements. Examples are sorting, searching, etc.<\/li>\n\n\n\n<li><strong>Containers<\/strong>: Containers store objects and data. There are in total seven standard \u201cfirst-class\u201d container classes and three container adaptor classes and only seven header files that provide access to these&nbsp; container adaptors.<\/li>\n\n\n\n<li><strong>Functions<\/strong>: STL includes classes which overload the function call operator. Instances of such classes are called functors.<\/li>\n\n\n\n<li><strong>Iterators<\/strong>: It is used for working upon a sequence of values.It provides generiality in STL.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-are-vectors-in-c\"><strong>What are Vectors in C++?<\/strong><\/h3>\n\n\n\n<p>Vectors are part of STL. Vectors in C++ are sequence containers representing arrays that can change their size during runtime. They use contiguous storage locations for their elements just as efficiently as in arrays, which means that their elements can also be accessed using offsets on regular pointers to its elements.<\/p>\n\n\n\n<p>Vectors are the dynamic arrays that are used to store data.It is different from arrays which store sequential data and are static in nature, Vectors provide more flexibility to the program. Vectors can adjust their size automatically when an element is inserted or deleted from it.<\/p>\n\n\n\n<p>Vectors are not ordered in C++. Vector elements are placed in adjacent storage and&nbsp; can be easily accessed and traversed across using iterators. In vectors, data is inserted at the end when we use push_back() function . Inserting an element at the end of a vector takes differential time, as sometimes there may be a need of extending the vector,&nbsp; but inserting the element at the beginning or at the middle takes linear time. Removing the last element takes only constant time because no resizing takes place.&nbsp;Check out this <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/c-tutorial\" target=\"_blank\" rel=\"noreferrer noopener\">free online C++ tutorial <\/a>to learn more and enhance your skills. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"declaration-of-vectors-in-c\"><strong>Declaration of Vectors in C++<\/strong><\/h3>\n\n\n\n<p>It is mandatory to include #include&lt;vector&gt; library before using vectors in C++.<\/p>\n\n\n\n<p>For Vector declaration we need to follow the below syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvector&lt; object_type &gt; vector_variable_name;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"initialization-of-vectors\"><strong>Initialization of Vectors<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Pushing the values one-by-one in vector using push_back():<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>All the elements that need to be stored in the vector are pushed back one-by-one in the vector using the push_back() method.&nbsp;<\/li>\n\n\n\n<li><strong>Syntax:<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvector_name.push_back(element_value);\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Using the overload constructor of the vector Class:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This method is used to populate a vector with multiple times the same value.<\/li>\n\n\n\n<li><strong>Syntax:<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvector&lt;object_type&gt; vector_name (number_of_repetition,element_value);\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Using Array:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This method uses array as a parameter to be passed in the vector constructor.<\/li>\n\n\n\n<li><strong>Syntax:<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvector&amp;lt;object_type&gt; vector_name {val1,val2,val3,....,valn};\n<\/pre><\/div>\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Using already initialized vector:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This method uses an already created vector to create a new vector with the same values.<\/li>\n\n\n\n<li>This method passes the begin() and end() of an already initialized vector.<br><br>|<br><strong>Syntax:<\/strong><br>vector&lt;object_type&gt; vector_name_1{val1,val2,...,valn};<\/li>\n<\/ul>\n\n\n\n<p>vector&lt;object_type&gt; vector_name_2(vector_name_1.begin(),vector_name_1.end())<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"various-functions-in-vectors-are\"><strong>Various Functions in Vectors are<\/strong><\/h3>\n\n\n\n<p><strong>Iterators:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>begin()<\/strong> \u2013&nbsp; It returns an iterator pointing to the first element in the vector.<\/li>\n\n\n\n<li><strong>end() <\/strong>- It returns an iterator pointing to the last element in the vector.<\/li>\n\n\n\n<li><strong>rbegin() <\/strong>- It returns a reverse iterator pointing to the last element in the vector.<\/li>\n\n\n\n<li>rend() - It returns a reverse iterator pointing to the element preceding the first element in the vector. Basically considered as a reverse end.<\/li>\n\n\n\n<li><strong>cbegin() <\/strong>- It returns a constant iterator pointing to the first element in the vector.<\/li>\n\n\n\n<li><strong>cend() <\/strong>- It returns a constant iterator pointing to the element that follows the last element in the vector.<\/li>\n\n\n\n<li><strong>crbegin() <\/strong>- It returns a constant reverse iterator pointing to the last element in the vector.<\/li>\n\n\n\n<li><strong>crend() <\/strong>- It returns a constant reverse iterator pointing to the element preceding the first element in the vector.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Code for Visualizing the use of Iterators:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/C++ Code to Visualize Use of Iterators in C++\n#include &lt;iostream&gt; \n#include &lt;vector&gt; \nusing namespace std;   \nint main() \n{ \n    vector&lt;int&gt; a; \/\/Declaration of vector in C++\n   \n   \/\/Initializing vector \u2018a\u2019 with values from 1 to 7\n    for (int i = 1; i &lt;=7 ; i++) \n        a.push_back(i); \n\n    \/\/Printing the Output of vector \u2018a\u2019 using iterators  begin() and end()  \n    cout &lt;&lt; \"Output of begin and end Function: \"; \n    for (auto i = a.begin(); i != a.end(); ++i) \n        cout &lt;&lt; *i &lt;&lt; \" \"; \n   \n   \/\/Printing the Output of vector \u2018a\u2019 using iterators cbegin() and cend()\n    cout &lt;&lt; \"\\nOutput of cbegin and cend Function: \"; \n    for (auto i = a.cbegin(); i != a.cend(); ++i) \n        cout &lt;&lt; *i &lt;&lt; \" \"; \n   \n   \/\/Printing the output of vector \u2018a\u2019 using iterators rbegin() and rend()\n    cout &lt;&lt; \"\\nOutput of rbegin and rend Function: \"; \n    for (auto ir = a.rbegin(); ir != a.rend(); ++ir) \n        cout &lt;&lt; *ir &lt;&lt; \" \"; \n  \n    \/\/Printing the output of vector \u2018a\u2019 using iterators crbegin() and crend()\n    cout &lt;&lt; \"\\nOutput of crbegin and crend Function: \"; \n    for (auto ir = a.crbegin(); ir != a.crend(); ++ir) \n        cout &lt;&lt; *ir &lt;&lt; \" \"; \n  \n    return 0; \n} \n\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/SNENefBg1AE-bF8uW3Ab9KxPLepSBb8EkiC6JKbbITMtmWwI-d-t0SdMeXkVCt3o374vdwOI8r_XkDHOQ4C19LdYorKc7mSsnMxWec0YoIGGUibM8JVZagx4zf_SR8TMnYTPdsE\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Capacity:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>size() <\/strong>- It returns the number of elements currently present in the vector.<\/li>\n\n\n\n<li><strong>max_size() <\/strong>- It returns the maximum number of elements that a vector can hold.<\/li>\n\n\n\n<li><strong>capacity() <\/strong>- It returns the storage capacity currently allocated to the vector.<\/li>\n\n\n\n<li><strong>resize(n)<\/strong> - It resizes the container to store \u2018n\u2019 elements.<\/li>\n\n\n\n<li><strong>empty() <\/strong>- It returns whether the container is empty or not.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Code for visualizing the use of capacity functions:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/C++ program to demonstrate working of capacity function\n#include &lt;iostream&gt;\n#include &lt;vector&gt; \nusing namespace std; \nint main() \n{ \n   \/\/Declaring vector \u2018a\u2019 of integer type \n   vector&lt;int&gt; a; \n   \n   \/\/Initializing vector \u2018a\u2019 with values from 1 to 5\n    for (int i = 1; i &lt;= 5; i++) \n        a.push_back(i); \n  \n    \/\/Printing size of the vector \u2018a\u2019\n    cout &lt;&lt; \"Size : \" &lt;&lt; a.size(); \n   \n    \/\/Printing the Capacity of the vector \u2018a\u2019\n    cout &lt;&lt; \"\\nCapacity : \" &lt;&lt; a.capacity(); \n    \n    \/\/Printing the maximum size of the vector \u2018a\u2019\n    cout &lt;&lt; \"\\nMax_Size : \" &lt;&lt; a.max_size(); \n  \n    \/\/ resizing  the vector \u2018a\u2019 to  size  4 \n    a.resize(4); \n  \n    \/\/ printing the vector \u2018a\u2019 size after resize() function\n    cout &lt;&lt; \"\\nSize : \" &lt;&lt; a.size(); \n  \n    \/\/ checks if the vector is empty or not \n    if (a.empty() == false) \n        cout &lt;&lt; \"\\nVector is not empty\"; \n    else\n        cout &lt;&lt; \"\\nVector is empty\"; \n  \n  \n    return 0; \n} \n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/d2tlXaZ7r2AiBOPO0S5FIA1zUat2Cpp0DgnxymKGoO6ffQQDdAn9U2ocMGaBCVvcUvbmymFkz2y4MW4EEWstRNsl4gyM75a78TQcOwOlYnf-6xQHPH8cdgQt3OeJRMYBe_sfLwU\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Modifiers:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>assign() - <\/strong>It assigns a new value to the existing elements of the vector.<\/li>\n\n\n\n<li>push_back() - It pushes the element from back in the vector.<\/li>\n\n\n\n<li>pop_back() - It removes elements from the back of the vector.<\/li>\n\n\n\n<li>insert() - It inserts an element before a specified element in the vector.<\/li>\n\n\n\n<li>erase() - It is used to remove elements from a specified element or a range in the vector.<\/li>\n\n\n\n<li>swap() - It is used to swap the contents of two vectors of the same datatype. The sizes of vectors may differ.<\/li>\n\n\n\n<li><strong>clear() - <\/strong>It is used to remove all the elements from the vector.<\/li>\n<\/ul>\n\n\n\n<p>&nbsp;<strong>Example code to Visualize the Modifiers Function in vector:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/C++ code to visualize the Modifiers function in vectors\n#include &lt;bits\/stdc++.h&gt; \n#include &lt;vector&gt; \nusing namespace std; \n  \nint main() \n{ \n    \/\/ Declaring Vector \u2018a\u2019 of integer type\n    vector&lt;int&gt; a; \n  \n    \/\/ filling vector \u2018a\u2019 with 7 in repetition of 4 times\n    a.assign(4, 7); \n    \n    \/\/Printing the vector \u2018a\u2019 contents\n    cout &lt;&lt; \"The vector contains: \"; \n    for (int i = 0; i &lt; a.size(); i++) \n        cout &lt;&lt; a&#091;i] &lt;&lt; \" \"; \n  \n    \/\/ inserting 10 to the last position of vector \u2018a\u2019\n    a.push_back(10); \n    int n = a.size(); \n    cout &lt;&lt; \"\\nThe last element is: \" &lt;&lt; a&#091;n - 1]; \n  \n    \/\/ removing the last element from vector \u2018a\u2019\n    a.pop_back(); \n  \n    \/\/ printing the vector \u2018a\u2019 contents\n    cout &lt;&lt; \"\\nThe vector contains: \"; \n    for (int i = 0; i &lt; a.size(); i++) \n        cout &lt;&lt; a&#091;i] &lt;&lt; \" \"; \n  \n    \/\/ inserting 3 at the beginning of vector \u2018a\u2019\n    a.insert(a.begin(), 3); \n   \n   \/\/Printing the first element of vector \u2018a\u2019\n    cout &lt;&lt; \"\\nThe first element is: \" &lt;&lt; a&#091;0]; \n  \n    \/\/ removing the first element \n    a.erase(a.begin()); \n    \n   \/\/Printing the new first element of vector \u2018a\u2019\n    cout &lt;&lt; \"\\nThe first element is: \" &lt;&lt; a&#091;0]; \n  \n   \n    \/\/ erasing the vector \n    a.clear(); \n   \n    \/\/printing the vector \u2018a\u2019 after erasing it\n    cout &lt;&lt; \"\\nVector size after erase(): \" &lt;&lt; a.size(); \n  \n    \/\/ Creating two vectors \u2018a1\u2019 and \u2018a2\u2019 of integer type\n    vector&lt;int&gt; a1, a2; \n   \n\n    \/\/Pushing values in vector \u2018a1\u2019 and \u2018a2\u2019\n    a1.push_back(3); \n    a1.push_back(4); \n    a2.push_back(5); \n    a2.push_back(6); \n  \n   \/\/printing vector \u2018a1\u2019\n    cout &lt;&lt; \"\\n\\nVector 1 is: \"; \n    for (int i = 0; i &lt; a1.size(); i++) \n        cout &lt;&lt; a1&#091;i] &lt;&lt; \" \"; \n  \n    \/\/printing vector \u2018a2\u2019\n    cout &lt;&lt; \"\\nVector 2 is: \"; \n    for (int i = 0; i &lt; a2.size(); i++) \n        cout &lt;&lt; a2&#091;i] &lt;&lt; \" \"; \n  \n    \/\/ Swaping vectors \u2018a1\u2019 and \u2018a2\u2019 \n    a1.swap(a2); \n  \n   \/\/Printing vector \u2018a1\u2019 after swapping with \u2018a2\u2019\n    cout &lt;&lt; \"\\nAfter Swap \\nVector 1 is: \"; \n    for (int i = 0; i &lt; a1.size(); i++) \n        cout &lt;&lt; a1&#091;i] &lt;&lt; \" \"; \n  \n    \/\/printing vector \u2018a2\u2019 after swapping with \u2018a1\u2019\n    cout &lt;&lt; \"\\nVector 2 is: \"; \n    for (int i = 0; i &lt; a2.size(); i++) \n        cout &lt;&lt; a2&#091;i] &lt;&lt; \" \"; \n    return 0;\n} \n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/SXE5y4P9GEu1u8_jYWEdwli5DBNTxsaXnsLINcj4Np2LETN27oJ0c-TFI8K8jZPEL6H---ZnrKJjM4VpbAY99AqJ8tUT6exnZzldcCSF1SmnEjm1Q_E8m93NtJH5GFrVJ2FuIqM\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Element access:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>reference_operator[g]: <\/strong>It returns a reference to the \u2018g\u2019 element in the vetor.<\/li>\n\n\n\n<li><strong>at(g): <\/strong>It returns a reference to the element at position \u2018g\u2019 in the vector.<\/li>\n\n\n\n<li><strong>front(): <\/strong>It returns a reference to the first element in the vector.<\/li>\n\n\n\n<li><strong>back(): <\/strong>It returns a reference to the last element in the vector.<\/li>\n\n\n\n<li><strong>data(): <\/strong>It returns a direct pointer to the memory array which is used internally by the vector to store its owned elements.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example code to visualize the Element access function in C++:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;bits\/stdc++.h&gt; \nusing namespace std; \n  \nint main() \n{ \n   \/\/Declaring vector \u2018a\u2019 of integer type \n   vector&lt;int&gt; a; \n    \n    \/\/pushing values fn vector \u2018a\u2019 from 10 to 100\n    for (int i = 1; i &lt;= 10; i++) \n        a.push_back(i * 10); \n  \n   \/\/Using reference operator to print vector \u2018a\u2019 third element\n    cout &lt;&lt; \"\\nReference operator &#091;g] : a&#091;2] = \" &lt;&lt; a&#091;2]; \n   \n   \/\/printing element at index 4 of the vector \u2018a\u2019\n    cout &lt;&lt; \"\\nat : a.at(4) = \" &lt;&lt; a.at(4); \n   \n   \/\/printing the front element of vector \u2018a\u2019\n    cout &lt;&lt; \"\\nfront() : a.front() = \" &lt;&lt; a.front(); \n  \n   \/\/printing the back element of vector \u2018a\u2019\n    cout &lt;&lt; \"\\nback() : a.back() = \" &lt;&lt; a.back(); \n  \n    \/\/ pointer to the first element \n    int* pos = a.data(); \n    \n   \/\/printing the first element of vector using pointer \u2018pos\u2019\n    cout &lt;&lt; \"\\nThe first element is \" &lt;&lt; *pos; \n    return 0; \n} \n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/r8woR_lj1FY1ii4LlBIXKHJFEixiccZaYnEbA_cvFAuR0jXLLzxW4rcgwgHBVlQ-yFHmFLUk6I8Rlbxe8p-RYmyxsWwVt-lM-DZCku7m_l2gR1Fy4T7fWpeD5CGyrFFU_5_24Uo\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"allocators-function-in-vector-c\"><strong>Allocators Function in Vector C++<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An allocator is an object that dynamically allocates and deallocates memory.<\/li>\n\n\n\n<li>In C++ vectors, there is only one function which can be used as an allocator. This function is called the <strong>get_allocator()<\/strong> function. We basically use the get_allocator() to allocate chunks of memory which in return a copy of the allocator object associated with the vector.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"when-to-use-vectors\"><strong>When to use Vectors?<\/strong><\/h3>\n\n\n\n<p><strong><\/strong>We can use Vectors in the following circumstances:<\/p>\n\n\n\n<p>It is advisable to use vectors when data are consistently changing.<\/p>\n\n\n\n<p>If the size of data is unknown then it is advisable to use vectors.<\/p>\n\n\n\n<p>It is advisable to use vectors when elements are not predefined.<\/p>\n\n\n\n<p>Compared to arrays there are more ways to copy vectors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"vectors-and-array-in-c\"><strong>Vectors and Array in C++<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Vector is a sequential container.<\/li>\n\n\n\n<li>Vector is not index based.<\/li>\n\n\n\n<li>Array is a fixed-size sequential collection of elements of the same type.<\/li>\n\n\n\n<li>Array is index based.<\/li>\n\n\n\n<li>Vectors are dynamic in nature.<\/li>\n\n\n\n<li>Once the array is initialized it\u2019s size can\u2019t be changed.<\/li>\n\n\n\n<li>Vector occupies more memory as compared to array.<\/li>\n\n\n\n<li>Array is memory efficient data structure.<\/li>\n\n\n\n<li>Accessing time in vectors is more.<\/li>\n\n\n\n<li>Array elements are arranged in contiguous memory allocation so it accesses elements in constant time.<\/li>\n\n\n\n<li>Vectors can be only declared in C++.<\/li>\n\n\n\n<li>Arrays can be declared in any programming language like C, Java, Python, etc.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"vector-of-vectors-in-c-stl\"><strong>Vector of Vectors in C++ STL<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Vector of Vectors is a two-dimensional vector with a variable number of rows where each row is considered as a vector.&nbsp;<\/li>\n\n\n\n<li>Each index of vector stores a vector in it. It can be accessed or traversed using iterators.<\/li>\n\n\n\n<li>Basically, it can be considered as the array of vectors with dynamic properties.<\/li>\n\n\n\n<li><strong>Syntax:<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n- vector&lt;vector&lt;data_type&gt;&gt; vector_name;\n<\/pre><\/div>\n\n\n<p><strong>Example code to visualize Vector of Vectors in C++:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/C++ program to visualize Vector of Vectors \n#include &lt;iostream&gt; \n#include &lt;vector&gt; \nusing namespace std; \n  \n\/\/ Defining the rows and columns of \n\/\/ vector of vectors \n#define row 3\n#define col 3\n  \nint main() \n{ \n    \/\/ Initializing the vector of vectors \n    vector&lt;vector&lt;int&gt; &gt; a; \n  \n    \/\/ Elements to insert in column \n    int num = 10; \n  \n    \/\/ Inserting elements into vector \n    for (int i = 0; i &lt; row; i++) { \n        \/\/ Vector to store column elements \n        vector&lt;int&gt; a1; \n       \n       \/\/pushing values in vector \u2018a1\u2019\n        for (int j = 0; j &lt; col; j++) { \n            a1.push_back(num); \n            num += 5; \n        } \n  \n        \/\/ for creating a 2D vector \u2018a\u2019 pushing 1D vector \u2018a1\u2019 into it\n        a.push_back(a1); \n    } \n  \n    \/\/ Displaying the 2D vector \u2018a\u2019\n    cout&lt;&lt;\"2D vector contains:\"&lt;&lt;\"\\n\";\n    for (int i = 0; i &lt; a.size(); i++) { \n        for (int j = 0; j &lt; a&#091;i].size(); j++) \n            cout &lt;&lt; a&#091;i]&#091;j] &lt;&lt; \" \"; \n        cout &lt;&lt; endl; \n    } \n    \/\/Deleting in 2D vector \u2018a\u2019\n    a&#091;2].pop_back(); \n    a&#091;1].pop_back(); \n    \n    cout&lt;&lt;\"2D vector traversal after deletion:\"&lt;&lt;\"\\n\";\n    \/\/Traversing in 2D vector \u2018a\u2019  using iterator after deletion\n    for (int i = 0; i &lt; a.size(); i++) { \n        for ( \n            auto it = a&#091;i].begin(); \n            it != a&#091;i].end(); it++) \n            cout &lt;&lt; *it &lt;&lt; \" \"; \n        cout &lt;&lt; endl; \n    } \n    return 0; \n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/nVlJVmKYLbY583M1dIv8p9c6vcmFGo25aF5M0Nj4Ze4KMBh6kqaoX5EM9qwOwWQKHouqiFHCNHTRz-drGunKGYCvZS-dJVJPgOeHzRwvrEYtuqrCPMgG4cJrKog2XOAmt86uGsY\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"advantages-of-vectors\"><strong>Advantages of Vectors<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is dynamic in nature.<\/li>\n\n\n\n<li>Elements can be inserted, deleted easily.<\/li>\n\n\n\n<li>Multiple objects can be stored.<\/li>\n\n\n\n<li>It is very easy to copy vectors from one to another by just using assignment operator.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"disadvantages-of-vectors\"><strong>Disadvantages of Vectors<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Memory consumption is more.<\/li>\n\n\n\n<li>It is not indexed.<\/li>\n\n\n\n<li>It doesn't use contiguous memory.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"advantages-of-arrays\"><strong>Advantages of Arrays<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It supports random access to it\u2019s&nbsp; members.<\/li>\n\n\n\n<li>It is more appropriate in storing a fixed number of elements.<\/li>\n\n\n\n<li>Uses contiguous memory.&nbsp;<\/li>\n\n\n\n<li>It is indexed.<\/li>\n\n\n\n<li>It is easy to sort in arrays.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"disadvantages-of-arrays\"><strong>Disadvantages of Arrays<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dynamic creation of arrays is not possible.<\/li>\n\n\n\n<li>It cannot store multiple data types.<\/li>\n\n\n\n<li>Deletion of elements is not easy.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"summary\"><strong>Summary<\/strong><\/h3>\n\n\n\n<p>As we have learned about C++ vectors, it is clear that it is a<a href=\"https:\/\/www.mygreatlearning.com\/blog\/data-structure-tutorial-for-beginners\/\"> data structure<\/a> that not only acts as a dynamic array but also ensures quick and random access of elements pertaining to that vector. Now, you can easily insert, delete, traverse, and modify elements in vectors as well as manage computer memory in an efficient manner.We can now understand where to apply vectors and where to apply arrays in a program.Vectors are an important concept for every C++ professional.<\/p>\n\n\n\n<p>With this, we come to the end of this article on Vectors in C++. I hope you got an idea of how to use and where to use vectors in C++ programs. I hope you all got an idea of how arrays differ from vectors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to programming in C++, vectors are an essential data structure that every developer should be familiar with. Vectors provide a dynamic array-like structure that can store and manipulate elements efficiently. Whether you are a beginner or an experienced programmer, understanding vectors in C++ is crucial for building robust and flexible applications. In [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":26643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[],"content_type":[],"class_list":["post-28580","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Vectors in C++ STL<\/title>\n<meta name=\"description\" content=\"Vectors in C++ are sequence containers representing arrays that can change their size during runtime. Learn more.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Vectors in C++ STL\" \/>\n<meta property=\"og:description\" content=\"Vectors in C++ are sequence containers representing arrays that can change their size during runtime. Learn more.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-30T06:29:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:38:23+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1254\" \/>\n\t<meta property=\"og:image:height\" content=\"837\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Vectors in C++ STL\",\"datePublished\":\"2023-05-30T06:29:55+00:00\",\"dateModified\":\"2024-09-03T05:38:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/\"},\"wordCount\":1616,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1075599562-1.jpg\",\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/\",\"name\":\"Vectors in C++ STL\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1075599562-1.jpg\",\"datePublished\":\"2023-05-30T06:29:55+00:00\",\"dateModified\":\"2024-09-03T05:38:23+00:00\",\"description\":\"Vectors in C++ are sequence containers representing arrays that can change their size during runtime. Learn more.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1075599562-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1075599562-1.jpg\",\"width\":1254,\"height\":837,\"caption\":\"code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/vectors-in-c\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Vectors in C++ STL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Vectors in C++ STL","description":"Vectors in C++ are sequence containers representing arrays that can change their size during runtime. Learn more.","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:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Vectors in C++ STL","og_description":"Vectors in C++ are sequence containers representing arrays that can change their size during runtime. Learn more.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-05-30T06:29:55+00:00","article_modified_time":"2024-09-03T05:38:23+00:00","og_image":[{"width":1254,"height":837,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Vectors in C++ STL","datePublished":"2023-05-30T06:29:55+00:00","dateModified":"2024-09-03T05:38:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/"},"wordCount":1616,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg","articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/","url":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/","name":"Vectors in C++ STL","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg","datePublished":"2023-05-30T06:29:55+00:00","dateModified":"2024-09-03T05:38:23+00:00","description":"Vectors in C++ are sequence containers representing arrays that can change their size during runtime. Learn more.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg","width":1254,"height":837,"caption":"code"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/vectors-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Vectors in C++ STL"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg",1254,837,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1-768x513.jpg",768,513,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1-1024x683.jpg",1024,683,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg",1254,837,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg",1254,837,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg",640,427,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg",96,64,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1075599562-1.jpg",150,100,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"When it comes to programming in C++, vectors are an essential data structure that every developer should be familiar with. Vectors provide a dynamic array-like structure that can store and manipulate elements efficiently. Whether you are a beginner or an experienced programmer, understanding vectors in C++ is crucial for building robust and flexible applications. In&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/28580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=28580"}],"version-history":[{"count":17,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/28580\/revisions"}],"predecessor-version":[{"id":111486,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/28580\/revisions\/111486"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/26643"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=28580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=28580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=28580"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=28580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}