Programming-Idioms

Implementation
C++

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Implementation edit is for fixing errors and enhancing with metadata.

Instead of changing the code of the snippet, consider creating another C++ implementation.

Other implementations
j := 0
for i, v := range x {
	if p(v) {
		x[j] = x[i]
		j++
	}
}
x = x[:j]
for i, v := range x {
	if p(v) {
		x[j] = x[i]
		j++
	}
}
for k := j; k < len(x); k++ {
	x[k] = nil
}
x = x[:j]
classes
for i := x.count-1 downto 0 do
  if not p(x.items[i]) then x.delete(i);
#include <list>
#include <functional>
std::list<Foo> x;
x.remove_if(std::not_fn(p));
x.select!(&:p)
for (const [key, value] of x.entries()) {
	if (!p(value)) x.splice(key, 1);
}
let mut j = 0;
for i in 0..x.len() {
    if p(x[i]) {
        x[j] = x[i];
        j += 1;
    }
}
x.truncate(j);