Se il mosso-da elementi in grado di rimanere dove sono in first
, poi basta usare copy_if
con move_iterator
.
std::copy_if(std::make_move_iterator(first.begin()),
std::make_move_iterator(first.end()),
std::back_inserter(second), cond);
Se il mosso-da elementi devono essere cancellate dal first
, farei
// partition: all elements that should not be moved come before
// (note that the lambda negates cond) all elements that should be moved.
// stable_partition maintains relative order in each group
auto p = std::stable_partition(first.begin(), first.end(),
[&](const auto& x) { return !cond(x); });
// range insert with move
second.insert(second.end(), std::make_move_iterator(p),
std::make_move_iterator(first.end()));
// erase the moved-from elements.
first.erase(p, first.end());
O partition_copy
con un move_iterator
, seguito da assegnazione:
std::vector<T> new_first;
std::partition_copy(std::make_move_iterator(first.begin()),
std::make_move_iterator(first.end()),
std::back_inserter(second), std::back_inserter(new_first), cond);
first = std::move(new_first);
fonte
2015-08-22 12:12:19
Come su [ 'std :: copy_if'] (http://en.cppreference.com/w/cpp/algorithm/ copia) seguito da ['std :: remove_if'] (http://en.cppreference.com/w/cpp/algorithm/remove)? –
@JoachimPileborg Perché pensi che 'remove_copy_if' faccia il lavoro? – 0xbadf00d
'copy_if' con' move_iterator'. Assicurati che il tuo predicato stia prendendo la sua argomentazione da const ref. –