Prerequisites
Vector of pointers are vectors that can hold multiple pointers. Each pointer within a vector of pointers points to an address storing a value. We can use the vector of pointers to manage values that are not stored in continuous memory.
How to Create Vector of Pointers in C++?
Similar to any other vector declaration we can declare a vector of pointers. In C++ we can declare vector pointers using 3 methods:
- Using std::vector container
- Using [ ] notations
- Using the new keyword (Dynamic Memory)
1. Using std::vector container
Using vectors to create vector pointers is the easiest and most effective method as it provides extra functionality of STL.
vector<int *> v1 ; //vector with integer pointers
vector<float *> v2; //vector with float pointers
vector<string *> v3; //vector with string pointers
A. Insert elements in a vector:
We can insert elements by 2 methods:
- while initialization
- using push_back( )
Insertion while initialization: Although it’s an option that can be used we should avoid such type of insertion as vectors store addresses within them.
Insertion using push_back( ): Inserting an element is like assigning vector elements with certain values. We can perform this task in certain steps.
- Create a variable and insert a value in it.
- Insert the address of the variable inside the vector.
B. Deletion of Elements
Deletion of the element is not as simple as pop_back in the case of pointers. It can be done using 2 steps:
- Free the pointer (Remove address from variable)
- Erase the variable.
C++
#include<bits/stdc++.h>
using namespace std;
void insert_element(vector< int *>& v, int i)
{
int a;
cin >> a;
v[i] = new int (a);
}
void print_vector(vector< int *>& v)
{
for ( int i = 0; i < v.size(); i++) {
cout << *(v[i]) << " " ;
}
cout << endl;
}
void delete_element(vector< int *>& v, int pos)
{
if (pos <= 0 || pos > v.size())
return ;
pos = pos - 1;
delete v[pos];
v.erase(v.begin() + pos);
}
int main()
{
cout << "Enter size of vector: " ;
int n;
cin >> n;
vector< int *> v(n, nullptr);
cout << "Enter elements of vector: " ;
for ( int i = 0; i < n; i++) {
insert_element(v, i);
}
cout << "Before: " ;
print_vector(v);
cout << "Enter position to remove: " ;
int pos;
cin >> pos;
delete_element(v, pos);
cout << "After: " ;
print_vector(v);
return 0;
}
|
Output:
Enter size of vector: 5
Enter elements of vector: 5 4 3 2 1
Before: 5 4 3 2 1
Enter position to remove: 3
After: 5 4 2 1
2. Using [ ] notations
Square brackets are used to declare fixed size. This can be used to operate over to create an array containing multiple pointers. This is a type of array that can store the address rather than the value. So, can be called a pointer array, and the memory address is located on the stack memory rather than the heap memory.
Syntax:
int *arr[size_of_arr];
Example:
C++
#include <iostream>
using namespace std;
void remove_element( int ** arr, int * n)
{
int pos = *n;
arr[pos - 1] = NULL;
*n = (*n) - 1;
}
void print_elements( int ** arr, int n)
{
for ( int i = 0; i < n; i++) {
cout << *(arr[i]) << " " ;
}
cout << endl;
}
int main()
{
cout << "Enter the size of array: " ;
int n;
cin >> n;
int * arr[n];
int input[n];
cout << "Enter the elements of the array: " ;
for ( int i = 0; i < n; i++) {
cin >> input[i];
arr[i] = &input[i];
}
cout << "Elements before: \n" ;
print_elements(arr, n);
remove_element(arr, &n);
cout << "Elements after removal: \n" ;
print_elements(arr, n);
}
|
Output:
Enter the size of array: 5
Enter the elements of the array: 5 4 3 2 1
Elements before:
5 4 3 2 1
Elements after:
5 4 3 2
3. Using the new keyword
The new Keyword in C++ represents dynamic memory allocation i.e, heap memory. The code will suffer from a memory leak if the programmer does not free up the memory before exiting. This can lead to a huge problem in long-running applications or resource-constrained hardware environments.
Syntax:
int **arr=new int*[size_of_array];
Example:
C++
#include <iostream>
#include <vector>
using namespace std;
void print_vector( int ** ptr, int n)
{
cout << "Elements are: " ;
for ( int i = 0; i < n; i++) {
cout << **(ptr + i) << " " ;
}
cout << endl;
}
void remove_element( int ** ptr, int * n)
{
int pos = *n - 1;
delete ptr[pos];
(*n)--;
}
int main()
{
cout << "Enter size of array: " ;
int n;
cin >> n;
int ** ptr = new int *[n];
cout << "Enter elements of array:" ;
for ( int i = 0; i < n; i++) {
int a;
cin >> a;
*(ptr + i) = new int (a);
}
cout << "Array Before removing element\n" ;
print_vector(ptr, n);
remove_element(ptr, &n);
cout << "Array After removing element\n" ;
print_vector(ptr, n);
for ( int i = 0; i < n; i++) {
delete ptr[i];
}
delete ptr;
}
|
Output:
Enter size of array: 5
Enter elements of array: 5 4 3 2 1
Array Before removing element
Elements are: 5 4 3 2 1
Array After removing element
Elements are: 5 4 3 2
Last Updated :
26 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...