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++
// 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
Similar Reads
Vector of Strings in C++ In C++, a vector of strings is a std::vector container that stores multiple strings. It is useful when you need to store a collection of string data in a single container and refer to them quickly. In this article, we will learn about the vector of strings and how to create and use it in C++.Table o
3 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
C++ Pointers A pointer is a variable that stores the address of another variable. Pointers can be used with any data type, including basic types (e.g., int, char), arrays, and even user-defined types like classes and structures.Create PointerA pointer can be declared in the same way as any other variable but wit
8 min read
Vector data() in C++ STL In C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam
2 min read
Vector Operator = in C++ STL In C++, the vector operator = is used to assign the contents of one vector to another. It allows you to copy elements from one vector to another or initialize a vector with another vector's contents.Letâs take a look at a simple code example:C++#include <bits/stdc++.h> using namespace std; int
3 min read
Sorting 2D Vector of Pairs in C++ A 2D vector also known as vector of vectors is a vector in which each element is a vector on its own. In other words, It is a matrix implemented with the help of vectors. What is a 2D vector of pairs? A 2D vector of pairs is a vector in which each element is a vector of pairs on its own. In other wo
15+ min read
Pointers vs Array in C++ Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e
3 min read
Copy File To Vector in C++ STL Prerequisite:Â Vectors in C++ STLFile Handling in C++ The C++ Standard Template Library (STL) provides several useful container classes that can be used to store and manipulate data. One of the most commonly used container classes is the vector. In this article, we will discuss how to copy the conte
2 min read
A C/C++ Pointer Puzzle Prerequisite: Pointers Assuming the size of int = 4 bytes, size of a pointer variable = 8 byte, what will be the output of following program. Few hints on how to solve it: Size of int = 4 bytes, size of a pointer variable = 8 bytes (on my machine), adding 1 to a pointer makes the pointer point to it
2 min read
List of Vectors in C++ STL In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { list<vector<int>> l = {{1, 3}, {2
3 min read