#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
/**
* Moves all elements in the input container
* that satisfy the given predicate
* into the output container,
* and erases them from the input container.
* @tparam Container The container type.
* @tparam UnaryPredicate The predicate type.
* @param in The input container.
* @param out The output container.
* @param predicate The predicate to satisfy.
*/
template <class Container, class UnaryPredicate>
void move_and_erase_if(Container &in, Container &out, UnaryPredicate predicate) {
// sort the input container so that all elements that
// satisfy the predicate are moved to the end.
auto it = std::partition(in.begin(), in.end(), predicate);
// move the elements satisfying the predicate
// into the output container.
std::move(in.begin(), it, std::back_inserter(out));
// erase the elements satisfying the predicate
// from the input container.
in.erase(in.begin(), it);
}
int main() {
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,6);
std::vector<int> diceRolls(100);
for (size_t i = 0; i < diceRolls.size(); i++) {
diceRolls[i] = distribution(generator);
}
std::vector<int> luckySixes;
move_and_erase_if(diceRolls, luckySixes, [](int val){
return val == 6;
});
std::cout << "DICE ROLLS: ";
for (auto val : diceRolls) {
std::cout << val << " ";
}
std::cout << "\nLUCKY SIXES: ";
for (auto val : luckySixes) {
std::cout << val << " ";
}
return 0;
}