C++

C++ Vector of Pointers Examples

An ordinary vector encountered in C++ programming, is a vector of objects of the same type. These objects can be fundamental objects or objects instantiated from a class. This article illustrates examples of vector of pointers, to same object type. To use a C++ vector, the program has to include the vector library, with a directive.

All the vector code for this article is in the main() function, unless otherwise indicated. Vector of pointers to different types, is however, addressed at the end of the article. In order to appreciate vector-of-pointers, it is good to recall the knowledge for vector of objects.

Article Content

Recall for Vector of Objects

Vector of Character Example
The following program, shows an example of a vector of chars:

    #include <iostream>
    #include <vector>
    using namespace std;
   
    int main()
    {
        vector vtr = {'U', 'V', 'W', 'X', 'Y', 'Z'};

        for (int i=0; i<vtr.size(); i++)
            cout << vtr[i] << ' ';
        cout << endl;

        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout << *it << ' ';
        cout << endl;

        return 0;
    }

The output is:

U V W X Y Z
U V W X Y Z

The same list has been displayed twice. The first statement in the main() function, creates a vector of chars. The next two code segments, print out the same vector list at the terminal. The first of these code segments, uses indexes. The second uses iterators.

Vector of Integer Example
The following program, shows an example of a vector of ints:

    #include <iostream>
    #include <vector>
    using namespace std;
   
    int main()
    {
        vector vtr = {1, 2, 3, 4, 5, 6};

        for (int i=0; i<vtr.size(); i++)
            cout << vtr[i] << ' ';
        cout << endl;

        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout << *it << ' ';
        cout << endl;

        return 0;
    }

The output is:

1 2 3 4 5 6
1 2 3 4 5 6

The same list has been displayed twice, in the same way, as in the previous code.

Vector of String Object Example
A const pointer to chars, points to a string literal. A string object is instantiated from the string class. To have a string object, the string library has to be included with a directive, at the top of the program. It is possible and easy to have a vector of string objects, as the following program shows:

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
   
    int main()
    {
        vector vtr = {"one", "two", "three", "four", "five"};

        for (int i=0; i<vtr.size(); i++)
            cout << vtr[i] << ' ';
        cout << endl;

        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout << *it << ' ';
        cout << endl;

        return 0;
    }

The output is:

one two three four five
one two three four five

The same list has been displayed twice, in the same way, as in the previous code.

Vector of Instantiated Object Example
The programmer can create his own class, instantiate objects from the class, and then put the objects in a vector. The following program illustrates this:

    #include <iostream>
    #include <vector>
    using namespace std;

    class TheCla {
        public:
        const char* str;
        TheCla (char chs[]) {
            str = chs;
        }
    };

    int main()
    {
        char ch1[] = "text1", ch2[] = "text2", ch3[] = "text3", ch4[] = "text4", ch5[] = "text5";    
        TheCla obj1(ch1), obj2(ch2), obj3(ch3), obj4(ch4), obj5(ch5);
        vector vtr = {obj1, obj2, obj3, obj4, obj5};  

        for (int i=0; i<vtr.size(); i++)
            cout << vtr[i].str << ' ';
        cout << endl;
        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout <str << ' ';
        cout << endl;

        return 0;
    }

The output is:

text1 text2 text3 text4 text5
text1 text2 text3 text4 text5

The same list has been displayed twice, in the same way, as in the previous code.

The class has a constructor and one public data member. The constructor assigns its argument to this data member.

The values of the vector, this time, are string literals, and not string objects. So, note the way the literals and vector elements have been coded.

If an object instantiated from the class is obj, then the value of the member, str would be accessed through the object, as:

    obj.str

obj in this case is a reference. Note the use of the dot operator. This is why, in the last-but-one code segment, in the main() function, each vector value has been accessed as:

    vtr[i].str

where [i] is the index.

If a pointer to obj is “it”, then the value of the member, str would be accessed through the object, as:

    it->str

Note the use of the arrow operator here. The iterator is like a pointer. This is why, in the last code segment, each vector value has been accessed as:

    it->str

where “it” is the iterator.

Vector of Pointers of Same Type

Example for Vector of Pointers to Characters
The following program, shows an example of a vector of pointers to chars:

    #include <iostream>
    #include <vector>
    using namespace std;
   
    int main()
    {
        char ch1 = 'U', ch2 = 'V', ch3 = 'W', ch4 = 'X', ch5 = 'Y', ch6 = 'Z';

        vector vtr = {&ch1, &ch2, &ch3, &ch4, &ch5, &ch6};

        for (int i=0; i<vtr.size(); i++)
            cout << *vtr[i] << ' ';
        cout << endl;

        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout << **it << ' ';
        cout << endl;

        return 0;
    }

The output is:

U V W X Y Z
U V W X Y Z

The same list has been displayed twice. The first statement in the main() function creates 6 characters with their identifiers. The second statement represents these characters, with their addresses in memory; and that results in a vector of pointers to chars. Note the template argument of this vector. The next two code segments, print out the same vector list at the terminal. The first of these code segments uses indexes. The second uses iterators.

For the first of these code segments, since each element in the vector is a pointer, the index reference has to be dereferenced by the indirection operator, *.

The iterator is like a pointer. For the second of these code segments, since each element in the vector is a pointer, and the iterator is like a pointer, each element is seen as a pointer to pointer. And so each element has to be dereferenced twice, with **.

Example of Vector of Pointers to Integers
The following program, which is similar to the above, shows an example of a vector of pointers to ints:

    #include <iostream>
    #include <vector>
    using namespace std;
   
    int main()
    {
        int int1 = 1000, int2 = 2000, int3 = 3000, int4 = 4000, int5 = 5000, int6 = 6000;

        vector vtr = {&int1, &int2, &int3, &int4, &int5, &int6};

        for (int i=0; i<vtr.size(); i++)
            cout << *vtr[i] << ' ';
        cout << endl;

        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout << **it << ' ';
        cout << endl;

        return 0;
    }

The output is:

1000 2000 3000 4000 5000 6000
1000 2000 3000 4000 5000 6000

The same list has been displayed twice, in the same way, as of the previous code.

Example of Vector of Pointers to String Objects
A const pointer to chars, points to a string literal. A string object is instantiated from the string class. To have a string object, the string library has to be included with a directive, at the top of the program. It is possible and easy to have a vector of pointers to string objects, as the following program shows:

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
   
    int main()
    {
        string str1 = "aaa", str2 = "bbb", str3 = "ccc", str4 = "ddd", str5 = "eee", str6 = "fff";

        vector vtr = {&str1, &str2, &str3, &str4, &str5, &str6};

        for (int i=0; i<vtr.size(); i++)
            cout << *vtr[i] << ' ';
        cout << endl;

        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout << **it << ' ';
        cout << endl;

        return 0;
    }

The output is:

aaa bbb ccc ddd eee fff
aaa bbb ccc ddd eee fff

The same list has been displayed twice, in the same way, as of the previous code.

Vector of Pointers to User Defined Class Objects

The programmer can create his own class, instantiate objects from the class, and then put pointers to the objects, in a vector. The following program illustrates this:

    #include <iostream>
    #include <vector>
    using namespace std;

    class TheCla {
        public:
        const char* str;
        TheCla (char chs[]) {
            str = chs;
        }
    };

    int main()
    {
        char ch1[] = "text1", ch2[] = "text2", ch3[] = "text3", ch4[] = "text4", ch5[] = "text5";    
        TheCla obj1(ch1), obj2(ch2), obj3(ch3), obj4(ch4), obj5(ch5);

        vector vtr = {&obj1, &obj2, &obj3, &obj4, &obj5};  

        for (int i=0; i<vtr.size(); i++)
            cout <str << ' ';
        cout << endl;
        for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
            cout <str << ' ';
        cout << endl;

        return 0;
    }

The output is:

text1 text2 text3 text4 text5
text1 text2 text3 text4 text5

The same list has been displayed twice, in the same way, as in the previous code.

The class has a constructor and one public data member. The constructor assigns its argument to this data member.

The values of the vector, this time, are string literals, and not string objects. So, note the way the literals and vector elements, have been coded.

If an object instantiated from the class is obj, then the value of the member, str would be accessed through the object, as:

    obj.str

obj in this case is a reference. If a pointer to obj is ptr, then the value of the member, str would be accessed through the pointer, as:

    ptr->str

This is why in the last-but-one code segment, each vector value has been accessed as:

    vtr[i]->str

If ptrptr is a pointer to ptr (pointer to pointer), the value of the member, str would be accessed through the pointer, as:

    (*ptrptr)->str

The parentheses ensure that (*ptrptr) is evaluated first, instead of the possible first evaluation of (ptrptr->str).

The iterator is like a pointer. This is why in the last code segment, each vector value has been accessed as:

    (*it)->str

where “it” is the iterator.

Vector of Pointers to Different Types

To have a vector of pointers to different types, use the following procedure:

  • Let the template of the vector, be pointer to void.
  • Let the values of the vectors, be addresses of the different objects of different types.
  • When reading out the values, cast the void pointers to their appropriate types.

The following program illustrates these, with a char, int and string objects:

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;

    int main()
    {
        char ch = 'U';
        int inte = 1000;
        string str = "I love you.";
        vector vtr = {&ch, &inte, &str};

        cout << *((char*)vtr[0]) << endl;
        cout << *((int*)vtr[1]) << endl;
        cout << *((string*)vtr[2]) << endl;

        return 0;
    }

The output is:

U
1000
I love you.

Conclusion

A vector of pointers is similar to a vector of objects. The main differences are as follows: The values of the vector of pointers, have to be addresses of objects declared from or instantiated from the class. Assume that the class name is TheCla, then the template argument of the vector has to be “TheCla*”. The address of an object is obtained by preceding the identifier of the object, with & .

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.