Open In App

C++ Vector of Pointers

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1.  Using std::vector container 
  2.  Using [ ] notations 
  3.  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:

  1. Free the pointer (Remove address from variable)
  2. Erase the variable.

C++




// C++ Program to create
// vector of pointer
#include<bits/stdc++.h>
 
using namespace std;
 
void insert_element(vector<int*>& v, int i)
{
    // declaration and input of values of elements
    int a;
    cin >> a;
 
    // allocating address to i element
    v[i] = new int(a);
}
 
void print_vector(vector<int*>& v)
{
    // printing elements of the vector
    for (int i = 0; i < v.size(); i++) {
        cout << *(v[i]) << " ";
    }
 
    cout << endl;
}
 
void delete_element(vector<int*>& v, int pos)
{
    // Out of limit positions
    if (pos <= 0 || pos > v.size())
        return;
 
    // converting position into index number
    pos = pos - 1;
 
    // free the space from pointer
    delete v[pos];
 
    // removing element from the vector
    v.erase(v.begin() + pos);
}
 
int main()
{
    cout << "Enter size of vector: ";
 
    // size of vector
    int n;
    cin >> n;
 
    // create a vector
    vector<int*> v(n, nullptr);
 
    cout << "Enter elements of vector: ";
    for (int i = 0; i < n; i++) {
 
        // inserting n elements inside v vector
        insert_element(v, i);
    }
 
    cout << "Before: ";
    // printing vector
    print_vector(v);
 
    cout << "Enter position to remove: ";
 
    int pos;
    cin >> pos;
 
    // delete element from pos position
    delete_element(v, pos);
 
    cout << "After: ";
    // printing vector
    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++




// C++ Program to create
// pointer array
#include <iostream>
using namespace std;
 
void remove_element(int** arr, int* n)
{
    int pos = *n;
 
    // releasing the element
    arr[pos - 1] = NULL;
 
    // decreasing the size by one
    *n = (*n) - 1;
}
 
void print_elements(int** arr, int n)
{
    // printing elements
    for (int i = 0; i < n; i++) {
        cout << *(arr[i]) << " ";
    }
 
    cout << endl;
}
 
int main()
{
    cout << "Enter the size of array: ";
 
    // declaring size of array
    int n;
    cin >> n;
 
    // creating an pointer array
    int* arr[n];
    int input[n];
 
    cout << "Enter the elements of the array: ";
 
    // inserting elements in array
    for (int i = 0; i < n; i++) {
        cin >> input[i];
 
        arr[i] = &input[i];
    }
 
    cout << "Elements before: \n";
    print_elements(arr, n);
 
    // removing last element in array
    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++




// c++ Program to create
// vector pointer
// using new keyword (dynamic array)
#include <iostream>
#include <vector>
using namespace std;
 
void print_vector(int** ptr, int n)
{
    cout << "Elements are: ";
 
    // printing the values of the array
    for (int i = 0; i < n; i++) {
        cout << **(ptr + i) << " ";
    }
 
    cout << endl;
}
 
void remove_element(int** ptr, int* n)
{
    int pos = *n - 1;
 
    // releasing the element from the array
    delete ptr[pos];
 
    // decreasing the size of array
    (*n)--;
}
 
int main()
{
 
    cout << "Enter size of array: ";
    // declaring size of array;
    int n;
    cin >> n;
 
    // declaring dynamic array
    int** ptr = new int*[n];
 
    cout << "Enter elements of array:";
 
    // inserting elements in array
    for (int i = 0; i < n; i++) {
 
        // declaring a variable to store in array
        int a;
        cin >> a;
 
        // Inserting the declared variable in the array
        *(ptr + i) = new int(a);
    }
 
    cout << "Array Before removing element\n";
    print_vector(ptr, n);
 
    // remove last element from array
    remove_element(ptr, &n);
 
    cout << "Array After removing element\n";
    print_vector(ptr, n);
 
    // removing all elements of array
    for (int i = 0; i < n; i++) {
        delete ptr[i];
    }
 
    // releasing array
    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
Previous
Next
Share your thoughts in the comments
Similar Reads