. Advertisement .
..3..
. Advertisement .
..4..
It can be fairly complicated to remove element from array in C++ using the index. But this tutorial will show you to get this done, including suggestions for alternative data structures if this deletion occurs a lot.
Remove Element From Array In C++
C++ uses C-style arrays, which have a fixed size. This size is determined at compile time, and you can’t change it later, including adding or removing elements from the array. The number of its elements is constant and doesn’t change during the array’s lifetime.
That said, using a sequence of objects that can be shrunk or grown isn’t out of the question in C++.
std::memmove
While you can modify an existing array in C++, the values of its elements are totally accessible. You can, for instance, copy all of them to a new object, except for the element you want to delete. This will create a new array as you need.
The memmove()
function from the header file cstring can help you with this by copying a certain number of bytes from one place in memory to another. Its syntax:
void* memmove( void* dest, const void* src, std::size_t count );
Parameter:
- dest: the pointer that points to the memory location you want to copy to.
- src: the pointer that points to the memory location you want to copy from.
- count: the number of bytes
memmove()
needs to copy.
This is how you can implement this function to delete an element from a C++ array:
#include <iostream>
#include <array>
#include <cstring>
int main()
{
size_t array_size = 6;
int arr[array_size] = {0, 1, 2, 3, 4, 5};
std::cout << "Original array: ";
for (const int &n: arr) {
std::cout << n << ' ';
}
std::cout << "\n";
int index;
std::cout << "The index of the element you want to remove: ";
std::cin >> index;
size_t new_size = array_size - 1;
int new_arr[new_size];
std::memmove(new_arr, arr, index * sizeof(int));
std::memmove(new_arr + index, arr + index + 1, (new_size - index) * sizeof(int));
std::cout << "New string: ";
for (const int &m: new_arr) {
std::cout << m << ' ';
}
std::cout << "\n";
return 0;
}
Output:
Original array: 0 1 2 3 4 5
The index of the element you want to remove: 2
New string: 0 1 3 4 5
This program creates an array of 6 int values. It asks the user which element it should delete from this array. Note that this input is the index (from zero) of that element.
Before invoking the memmove()
function, we need a second array to hold the values copied from the first array. Its length should be one element smaller than that of the original array due to the deletion.
There are two copy operations required. First, we copy every element before the deleted element. Since this operation starts at the beginning of both arrays, we can use its name as the pointer to these memory locations.
To evaluate the number of bytes to copy, we multiply the size of int and the number of elements we need to copy. The second operation occurs after the deleted element to the end of those arrays.
std::erase
Instead of traditional arrays, you can use C++ vectors if you anticipate resizing it in many situations. These sequence containers act like dynamic arrays in many ways, allowing you to delete elements painlessly.
Available in C++20, the erase()
function is created for this exact purpose. It can remove one or multiple elements from a vector. To delete an element, you can use this syntax (in which position is the index of the element you want to remove from the vector):
erase(iterator position);
The following code shows how you can rewrite the previous example with this function:
#include <iostream>
#include <vector>
int main() {
size_t size = 6;
std::vector<int> arr = {0, 1, 2, 3, 4, 5};
std::cout << "Original vector: ";
for (const int &n: arr) {
std::cout << n << ' ';
}
std::cout << "\n";
int index;
std::cout << "The index of the element you want to remove: ";
std::cin >> index;
arr.erase(arr.begin() + index);
std::cout << "Modified vector: ";
for (const int &n: arr) {
std::cout << n << ' ';
}
std::cout << "\n";
}
Output:
Original vector: 0 1 2 3 4 5
The index of the element you want to remove: 2
Modified vector: 0 1 3 4 5
The usage of the erase()
function is fairly straightforward. You will just need to use the index, and the iterator begin()
, which points to the beginning of the vector.
Note: this guide addresses printing vectors in C++ – check it out.
Summary
The memmove()
function can remove element from array in C++, but it involves the creation of a new array. For easier element modification, you can use vectors and the erase()
function instead.
Leave a comment