I've got a question, how can I delete an element from an array?
I've already done research but delete[] ... didn't seem to work.
This is a small 'pseudo' code to explain what I mean and want to achieve:
1 2 3 4 5 6 7 8 9 10 11
int main() {
char text[250] = "Hello!";
char del = "e";
for (int i = 0; i < 7; i++) {
if (text[i] == del) {
text[i] = text[i] - del;
//Need help only with this part, rest is pseudo code!
}
}
}
The way i did it is find the position of the characher that I want to delete, then swap that postion with its next character til the char is at the last pos, or just swap it with the last character. Then delete it by replacng it with null character or recreate the new one with new size with new[] and delete[]
#include <iostream>
#include <algorithm>
#include <cstring>
int main()
{
char text[250] = "Hello!";
char del = 'e';
std::remove(text,
text + strlen(text) + 1, //1 to get trailing 0 as well
del);
std::cout << text;
}
↑ This does the same thing LendraDwi suggested under the hood. (Thought probably in more effective way)
↓ And Lowest0ne mage great visual of what happens.