(list)+filter function
This generates a new list, and appends to it every time the current list value satisfies the condition. It then recursively calls the filter on the next value.
This commit is contained in:
10
list.cpp
10
list.cpp
@@ -75,6 +75,16 @@ T reduce(List<T> *lst, T (*reducer) (T, T), T init = 0)
|
|||||||
return reduce(lst->next, reducer, init);
|
return reduce(lst->next, reducer, init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
List<T> *filter(List<T> *lst, bool (*f)(T), List<T> *new_lst = nullptr)
|
||||||
|
{
|
||||||
|
if (!lst)
|
||||||
|
return new_lst;
|
||||||
|
if (f(lst->value))
|
||||||
|
new_lst = append(new_lst, lst->value);
|
||||||
|
return filter(lst->next, f, new_lst);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::ostream& operator<<(std::ostream& ostream, const List<T> *lst)
|
std::ostream& operator<<(std::ostream& ostream, const List<T> *lst)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user