A way to remove items from the vector immer::vector<> #251
Replies: 2 comments 3 replies
-
Hi! For Cheers! |
Beta Was this translation helpful? Give feedback.
-
I decided to implement
template <typename T, typename Predicate>
::immer::flex_vector<T> erase_if_v1(::immer::flex_vector<T> v, Predicate pred) {
return std::accumulate(v.begin(), v.end(), ::immer::flex_vector<T>(),
[&](auto result, T const& next) {
if (!pred(next)) return result.push_back(next);
return result;
});
}
template <typename T, typename Predicate>
::immer::flex_vector<T> erase_if_v2(::immer::flex_vector<T> v, Predicate pred) {
for (auto i = 0u; i != v.size();) {
if (pred(v[i])) {
v = v.erase(i);
} else {
++i;
}
}
return v;
} I am not sure which one is better/preferable. The first I'd appreciate your comments/thoughts. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I've got a very basic question and I can't figure out a way to remove an item from the immer:vector<>. The whole immutability concept is new to me. I know that I can produce a new vector by inserting items I need, but still it's not the most efficient way. Maybe there is a function like
filter
to produce a new vector, but I can't find it.Could you please advise me on this?
Thanks a lot for your library and your effort :)
Best regards,
Sergii
Beta Was this translation helpful? Give feedback.
All reactions