how to delete array element in c++ code example
Example 1: how to delete something in an array c++
delete myarray[elemen];
Example 2: delete an array c++
#include <iostream>
using namespace std;
struct MyClass {
MyClass() {cout <<"MyClass constructed\n";}
~MyClass() {cout <<"MyClass destroyed\n";}
};
int main () {
MyClass * pt;
pt = new MyClass[3];
delete[] pt;
return 0;
}
int main () {
int * pt;
pt = new int [3];
delete[] pt;
return 0;
}
Example 3: delete index from array c++
const int SIZE = 9;
int arr[SIZE];
cout << "Enter numbers: \n";
for (int i = 0; i < SIZE; i++)
cin >> arr[i];
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
cout << "Enter Index U want to delete:\n";
int del;
cin >> del;
for (int i = del-1; i < SIZE; i++)
{
arr[del] = arr[del + 1];
del++;
}
for (int i = 0; i < SIZE - 1; i++)
cout << arr[i] << "\t";
cout << endl;