<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged pointer</title>
<link>https://coderlegion.com/tag/pointer</link>
<description>Powered by Question2Answer</description>
<item>
<title>Vector of pointers c++</title>
<link>https://coderlegion.com/23437/vector-of-pointers-c</link>
<description>&lt;div&gt;
    &lt;div id=&quot;toc-div&quot;&gt;
        &lt;h1&gt;
            Vector of pointers c++
        &lt;/h1&gt;
        &lt;p&gt;
            In this tutorial we gonna learn about vector of pointers. First we will see what are vectors and pointers. then in next section we are going to learn about the methods of how to create a vector of pointers. We basically gonna follow C++ language here.
        &lt;/p&gt;
        &lt;h4&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;A Table of Contents: &lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
        &lt;div id=&quot;toc-list&quot;&gt;
        &lt;ol&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Vectors&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Pointers&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why1&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;How to create the vector of Pointers in c++&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why2&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Method 1 : The new Operator is used to create Vector of Pointers in C++&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why3&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Method 2 : [] Notation is used to create Vector of Pointers in C++&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why3&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Method 3 : std::vector container is used to create Vector of Pointers in C++&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;&lt;/div&gt;
    &lt;/div&gt;
        
        
    &lt;a id=&quot;what&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Vectors&lt;/span&gt; &lt;a id=&quot;TypeError-str-object&quot; rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
    &lt;h3&gt;&lt;u&gt;Definition&lt;/u&gt;&lt;/h3&gt;
    &lt;p&gt;
        Vector is a template class in the Standard Template Library (STL) that function as a 
        refined array. Vectors can be resized themselves automatically when we insert or delete 
        elements, so they're great to use when working with constantly changing data. But in array we can resize it. Using vectors in our 
        program enables us to store our data with more flexibility and efficiency.
    &lt;/p&gt;
    &lt;div class=&quot;div-pink&quot;&gt;Let's examine how to define a vector in C++&lt;/div&gt;
    &lt;pre&gt;
        &lt;code&gt;
    #include 
    #include 
    using namespace std;
    
    int main() {
    
        vector vec;  
    
        vec.push_back(6); 
        vec.push_back(5); 
        vec.push_back(8);
        vec.push_back(0); 
        vec.push_back(5); 
    
        for (int i = 0; i &amp;lt; vec.size(); i++)
        {
        cout &amp;lt;&amp;lt; vec[i] &amp;lt;&amp;lt; endl; 
        }
        return 0; 
    }

        &amp;lt;/code&amp;gt;
    &lt;/code&gt;&lt;/pre&gt;
    &lt;p&gt;Output:&lt;/p&gt;
    &lt;pre&gt;
        &lt;code&gt;
            6
            5
            8
            0
            5
        &lt;/code&gt;
    &lt;/pre&gt;
    

    
    &lt;a id=&quot;why&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Pointers &lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
    &lt;h3&gt;&lt;u&gt;Definition&lt;/u&gt;&lt;/h3&gt;
    &lt;p&gt;
        Pointers are one of the most important aspects of C++. Pointers are another
        type of variables in CPP and these variables store addresses of other variables.
        While creating a pointer variable, we need to mention the type of data whose
        address is stored in the pointer .e.g. inorder to create a pointer which stores 
        address of an integer, we need to write:

                        &lt;br&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;b&gt;int  *p;&lt;/b&gt;&lt;/div&gt;&lt;br&gt;

        This means that p will contain address of an integer. So ,if a pointer is going to
        store address of datatype X, it will be declared like this:
                    
                   &lt;br&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;b&gt;X  *p;&lt;/b&gt;&lt;/div&gt;&lt;br&gt;
        Now let's say we have an integer i &amp;amp; an integer pointer p, we will use
        address of(&amp;amp;) operator inorder to put a ddress of i in p. Address of operator :&amp;amp;
        as studied abpveisaunary operator which returns address of a variable .e.g.
        &amp;amp;i will give us address of variable i. Here is the code to put address of  i in p.
                        &lt;br&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
                            &lt;b&gt;
                                int  i =10;&lt;br&gt;
                                int  *p;&lt;br&gt;
                                p=&amp;amp;i;
                            &lt;/b&gt;
                        &lt;/div&gt;


    
    &lt;div&gt;
        &lt;pre&gt;
            &lt;code&gt;
        #include
        using namespace std;

        int main()
        {
            int firstvalue= 5, secondvalue = 15;
            char thirdvalue= 'a';
            int *p1, *p2;
            char *p3;
            p1= &amp;amp;firstvalue; 
            p2= &amp;amp;secondvalue;
            p3= &amp;amp;thirdvalue;
            *p1= 10;
            *p2= *p1;
            p1= p2;
            *p1= 20;
            *p3= 'b';
            cout&amp;lt;&amp;lt;&quot;firstvalue is&quot; &amp;lt;&amp;lt; firstvalue&amp;lt;&amp;lt;'\n';
            cout&amp;lt;&amp;lt;&quot;secondvalue is&quot; &amp;lt;&amp;lt; secondvalue &amp;lt;&amp;lt;'\n';
            cout&amp;lt;&amp;lt;&quot;thirdvalue is&quot; &amp;lt;&amp;lt; thirdvalue&amp;lt;&amp;lt;'\n';
            return 0;
        }

            &amp;lt;/code&amp;gt;
        &lt;/code&gt;&lt;/pre&gt;
        &lt;p&gt;Outputs:&lt;/p&gt;
        &lt;pre&gt;
            &lt;code&gt;
                firstvalue is 10
                secondvalue is 20
                thirdvalue is b

            &lt;/code&gt;
        &lt;/pre&gt;
    &lt;/div&gt;
    
    
    
    &lt;a id=&quot;why1&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;How to create the vector of Pointers in c++ &lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
    &lt;p&gt;
        There are 3 methods for creating the vector of Pointers in c++
    &lt;/p&gt;
    &lt;ol&gt;
        &lt;li&gt;
            Method 1 : The new Operator is used to create Vector of Pointers in C++
        &lt;/li&gt;
        &lt;li&gt;
            Method 2 : [] Notation is used to create Vector of Pointers in C++
        &lt;/li&gt;
        &lt;li&gt;
            Method 3 : std::vector container is used to create Vector of Pointers in C++
        &lt;/li&gt;
    &lt;/ol&gt;
    
    &lt;a id=&quot;why2&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Method 1 : The new Operator is used to create Vector of Pointers in C++ &lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;

    &lt;p&gt;
        The new keyword is used to create a vector of pointers in dynamically allocated on the heap.
    &lt;/p&gt;
    &lt;div&gt;
        &lt;pre&gt;
            &lt;code&gt;
        #include 
        #include 
    
        using namespace std;
        
        
        int main()
        {
            int *pointers_vec = new int[8];
            cout &amp;lt;&amp;lt; &quot;vector pointers addresses are :&quot; &amp;lt;&amp;lt; endl;
            for(int i = 0; i &amp;lt; 8; ++i) {
                cout &amp;lt;&amp;lt; &amp;amp;pointers_vec[i] &amp;lt;&amp;lt; endl;
            }
            return 0;
        }

            &amp;lt;/code&amp;gt;
        &lt;/code&gt;&lt;/pre&gt;
        &lt;p&gt;
            Outputs:
        &lt;/p&gt;
        &lt;pre&gt;
            &lt;code&gt;
                vector pointers addresses are :
                0x2561c27
                0x2561c24
                0x2561c30
                0x2561c38
                0x2561c40
                0x2561c48
                0x2561c50
                0x2561c58
            &lt;/code&gt;
        &lt;/pre&gt;
    &lt;/div&gt;
    
    &lt;a id=&quot;why3&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Method 2 : [] Notation is used to create Vector of Pointers in C++&lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;

    &lt;p&gt;
        This method uses the array notation - [] that declare a constant-length array. It is similar to 
        regular array declaration, 
        but in this case, we are more interested 
        in accessing the each element's addresses. So we are using the &amp;amp;  operator to access 
        pointers in the vector. 
    &lt;/p&gt;
    &lt;div&gt;
        &lt;pre&gt;
            &lt;code&gt;
        #include 
        #include 
        
        using namespace std;
        
        
        int main()
        {
            int *pointers_vector[5];
        
            cout &amp;lt;&amp;lt; &quot;pointers vectors  addresses are:&quot; &amp;lt;&amp;lt; endl;
            for(auto &amp;amp;a : pointers_vector) {
                cout &amp;lt;&amp;lt; &amp;amp;a &amp;lt;&amp;lt; endl;
            }
            
            return 0;
        }

            &amp;lt;/code&amp;gt;
        &lt;/code&gt;&lt;/pre&gt;
        &lt;p&gt;Outputs:&lt;/p&gt;
        &lt;pre&gt;
            &lt;code&gt;
                pointers vectors  addresses are:
                0x7ffead7a00d0
                0x7ffead7a00e8
                0x7ffead7a00f8
                0x7ffead7a0100
                0x7ffead7a0108
            &lt;/code&gt;
        &lt;/pre&gt;
    &lt;/div&gt;
    
    &lt;a id=&quot;why4&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Method 3 : std::vector container is used to create Vector of Pointers in C++ &lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
    &lt;p&gt;
        std::vector offer a rich functionality to allocate vector of pointers and to manipulate the
         vector with multiple built-in functions. This method also provides a more flexible interface for 
         new element creation during the run-time. 
    &lt;/p&gt;
    &lt;div&gt;
        &lt;pre&gt;
            &lt;code&gt;
            #include 
            #include 
            
            using namespace std;
            
            int main()
            {
                vector vector_pointers(5, nullptr);
                cout &amp;lt;&amp;lt; &quot;vector_pointers addresses are:&quot; &amp;lt;&amp;lt; endl;
                for(int i = 0; i &amp;lt; 5; ++i) {
                    cout &amp;lt;&amp;lt; &amp;amp;vector_p[i] &amp;lt;&amp;lt; endl;
                }
                return 0;
            }


            &amp;lt;/code&amp;gt;
        &lt;/code&gt;&lt;/pre&gt;
        &lt;p&gt;
            Outputs:
        &lt;/p&gt;
        &lt;pre&gt;
            &lt;code&gt;
                vector_pointers addresses are:
                0x1255cf20
                0x1255cf28
                0x1255cf30
                0x1255cf38
                0x1255cf40
            &lt;/code&gt;
        &lt;/pre&gt;
    &lt;/div&gt;
    &lt;a id=&quot;con&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;The Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;
        
    &lt;p&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;And there you have it! You have seen  simple ways to create
        &lt;b&gt;vector of pointers in C++&lt;/b&gt;.Thank you for sticking with this tutorial all the 
        way to the end. Let us know if this post helped you to fix the problem. If you still have issues, 
        please add to the comments the error you are getting into. We would do our best to reply and help 
        you fix the problem.

        Cheers!
    &lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;    
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/23437/vector-of-pointers-c</guid>
<pubDate>Sat, 25 Jul 2026 06:59:43 +0000</pubDate>
</item>
</channel>
</rss>