-
Any c++ relevance here? It's c++ basic...
See for example std::remove_if()
-
Any c++ relevance here? It's c++ basic...
See for example std::remove_if()
@Christian-Ehrlicher i am confuse sir, how do you apply that in an array to remove a value of 0. Sorry
-
i have 10 elements in an array and i want to remove elements that is equal to zero
int proc[] = {pr1,pr2,pr3,pr4,pr5,pr6,pr7,pr8,pr9,pr10};
this is how i stored elements in an array.
@ioanna may I suggest you use QVector instead, ? Internally it uses an array as well but it offers a great number of functions to use on it.
removeAt
andremoveOne
are part of it, and what you're looking for.Alternatively you can go full c:
https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.htmlor use the std classes @Christian-Ehrlicher pointed to
-
@ioanna may I suggest you use QVector instead, ? Internally it uses an array as well but it offers a great number of functions to use on it.
removeAt
andremoveOne
are part of it, and what you're looking for.Alternatively you can go full c:
https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.htmlor use the std classes @Christian-Ehrlicher pointed to
-
int a[] = {1, 2, 3, 4, 5}; int arrayLenChange = std::end(a) - std::remove_if(std::begin(a), std::end(a), [](int v) { return v < 2; }); qDebug() << arrayLenChange; for (int i = 0; i < 5 - arrayLenChange; ++i) qDebug() << "a[" << i << "] =" << a[i];
-
You should use std::vector or QVector instead of C array in CPP, then find suitable <algorithm> method/function to solve the problem.
FYI, here is a quick reference:
https://en.cppreference.com/w/cpp/algorithm -
The question itself if pretty simple, but to read between the lines I have to wonder whether the proc array is the correct structure for the use case in the first place. Certainly not enough information in the OP to know WHY it is necessary to remove elements from the array, or if a linked-list structure would be a better generic solution. If I were keeping a list of active processes that can go away at any time and I needed to reap them from the list I'd use a hash map instead of an array.
If you stick to the original question as-is then the most optimal way of removing an unkown number of elements from the array involves a linear scan and copy to a new array.
-
@Kent-Dorfman said in How to remove an element in an array in c++?:
If you stick to the original question as-is then the most optimal way of removing an unkown number of elements from the array involves a linear scan and copy to a new array.
No, it depends on a lot of variables!
-
@Christian-Ehrlicher said in How to remove an element in an array in c++?:
No, it depends on a lot of variables!
In what world is that not the fastest general solution, given the conditions as defined? If you have a linear array with randomized values in it and need to find and remove all the elements that match a specific value then there is absolutely NO QUICKER way than a linear scan and copy to a new array. Anything else infers predictions about the ordeing of the data that we cannot make with the given information.
Edit: if the order of elements in the result is not important then the shrink can be done in-place by overwriting elements to be removed with elements from the end of the array, and adjusting the reported array size accordingly...but I challenge anyone to find me a more efficient general algorithms. My testing shows that compared to std:remove_if, shrink-by-linear-copy is 3 times as fast, and shrink-by-element-replacement is 15 times as fast.
-
When the element is movable and big enough I'm pretty sure your results will be much different.
-
When the element is movable and big enough I'm pretty sure your results will be much different.
@Christian-Ehrlicher said in How to remove an element in an array in c++?:
When the element is movable and big enough I'm pretty sure your results will be much different.
If it's big enough (whether it's movable is irrelevant), then the standard swap in-place on each removal is going to perform the worst. Move semantics can't magically prevent a copy, it's just a hint to the compiler where it can elide the copy, but you can't cheaply reorder memory regardless of the compiler knowing or not knowing if the element is movable. Just sayin'.
-
@ioanna may I suggest you use QVector instead, ? Internally it uses an array as well but it offers a great number of functions to use on it.
removeAt
andremoveOne
are part of it, and what you're looking for.Alternatively you can go full c:
https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.htmlor use the std classes @Christian-Ehrlicher pointed to
This post is deleted!