Vector of pointers c++
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.
A Table of Contents:
Vectors #
Definition
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.
Let's examine how to define a vector in C++
#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 < vec.size(); i++)
{
cout << vec[i] << endl;
}
return 0;
}
</code>
Output:
6
5
8
0
5
Pointers #
Definition
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:
int *p;
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:
X *p;
Now let's say we have an integer i & an integer pointer p, we will use
address of(&) operator inorder to put a ddress of i in p. Address of operator :&
as studied abpveisaunary operator which returns address of a variable .e.g.
&i will give us address of variable i. Here is the code to put address of i in p.
int i =10;
int *p;
p=&i;
#include
using namespace std;
int main()
{
int firstvalue= 5, secondvalue = 15;
char thirdvalue= 'a';
int *p1, *p2;
char *p3;
p1= &firstvalue;
p2= &secondvalue;
p3= &thirdvalue;
*p1= 10;
*p2= *p1;
p1= p2;
*p1= 20;
*p3= 'b';
cout<<"firstvalue is" << firstvalue<<'\n';
cout<<"secondvalue is" << secondvalue <<'\n';
cout<<"thirdvalue is" << thirdvalue<<'\n';
return 0;
}
</code>
Outputs:
firstvalue is 10
secondvalue is 20
thirdvalue is b
How to create the vector of Pointers in c++ #
There are 3 methods for creating the vector of Pointers in c++
-
Method 1 : The new Operator is used to create Vector of Pointers in C++
-
Method 2 : [] Notation is used to create Vector of Pointers in C++
-
Method 3 : std::vector container is used to create Vector of Pointers in C++
Method 1 : The new Operator is used to create Vector of Pointers in C++ #
The new keyword is used to create a vector of pointers in dynamically allocated on the heap.
#include
#include
using namespace std;
int main()
{
int *pointers_vec = new int[8];
cout << "vector pointers addresses are :" << endl;
for(int i = 0; i < 8; ++i) {
cout << &pointers_vec[i] << endl;
}
return 0;
}
</code>
Outputs:
vector pointers addresses are :
0x2561c27
0x2561c24
0x2561c30
0x2561c38
0x2561c40
0x2561c48
0x2561c50
0x2561c58
Method 2 : [] Notation is used to create Vector of Pointers in C++ #
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 & operator to access
pointers in the vector.
#include
#include
using namespace std;
int main()
{
int *pointers_vector[5];
cout << "pointers vectors addresses are:" << endl;
for(auto &a : pointers_vector) {
cout << &a << endl;
}
return 0;
}
</code>
Outputs:
pointers vectors addresses are:
0x7ffead7a00d0
0x7ffead7a00e8
0x7ffead7a00f8
0x7ffead7a0100
0x7ffead7a0108
Method 3 : std::vector container is used to create Vector of Pointers in C++ #
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.
#include
#include
using namespace std;
int main()
{
vector vector_pointers(5, nullptr);
cout << "vector_pointers addresses are:" << endl;
for(int i = 0; i < 5; ++i) {
cout << &vector_p[i] << endl;
}
return 0;
}
</code>
Outputs:
vector_pointers addresses are:
0x1255cf20
0x1255cf28
0x1255cf30
0x1255cf38
0x1255cf40
The Conclusion
And there you have it! You have seen simple ways to create
vector of pointers in C++.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!