How to move element in array in order or remove from array?
Example: arr[3,3,2,1]
move(arr[0],arr[sizeof(arr)]);
arr[3,2,1,3];
I wrote function, but is correctly it ?
void remove(int index, char *arr)
{
arr[index]=NULL;
for(int i=index;i<sizeof(arr)-1;i++)
swap(arr[i],arr[i+1]);
}
This already exists in the C++ standard library (
std::rotate), and the C standard library (memmove)If you’re using C++, (since it comes with a
std::swapfunction, and C can’t swap with parameters like that):Also, in C++, use a
std::vectororstd::stringrather than a nakedchar*pointer.If you’re using C:
If you are using naked
char*pointers, always pass a length.