delete in C++ code example
Example 1: c++ delet from memory
int* ptr1 = new int;
delete ptr1;
int* array = new int[10];
delete[] array;
Example 2: free or delete in c++
delete --> If you used the 'new' operator
free --> If you used 'malloc'
Example 3: c++ erase remove
std::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
v.erase(std::remove(v.begin(), v.end(), 5), v.end());
Example 4: 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;
}