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
Similar Reads
C++ Vector of Structs
Prerequisites: Structures in C++Vector in C++ Structures are user-defined datatypes used to group various related variables into one single data type. The structures can contain variables of different data types like int, string, boolean, etc. The variables are called the members of the structure. T
6 min read
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
3 min read
Vector insert() in C++ STL
In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function: [GFGTABS] C++ #include <bits/stdc++.
4 min read
void Pointer in C++
In C++, a void pointer is a pointer that is declared using the 'void' keyword (void*). It is different from regular pointers it is used to point to data of no specified data type. It can point to any type of data so it is also called a "Generic Pointer". Syntax of Void Pointer in C++void* ptr_name;
7 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
8 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 wi
9 min read
Smart Pointers in C++
In C++, pointers are the variables that stores the memory addresses. They are extensively used in dynamic memory location to store the address of allocated memory. But they bring a lot of issues. Problems with Normal PointersMemory Leaks: This occurs when memory is repeatedly allocated by a program
5 min read
vector swap() in C++
In C++, std::vector::swap() is a built-in function used to exchange the contents to two vectors of same type. This function does not copy, move or swap the individual elements, instead, it swaps the internal pointers to the dynamically allocated array of both vectors and updates the size accordingly
3 min read
Vector size() in C++ STL
In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method. Let's take a look at the simple code example: [GFGTABS] C++ #include
3 min read
2D Vector in C++
A 2D vector is a vector of the vector i.e. each element is a vector in itself. It can be visualised as a matrix where each inner vector represents a row, and the number of rows represents the maximum columns. A 2D vector is dynamically resizable in both dimensions. Letâs take a look at a simple illu
6 min read