You can also add an additional template parameter BinaryOp like this:
template <typename T, typename BinaryOperation> T fold(std::vector<T> nums, T acc, BinaryOperation op)
See the code. And its also slightly faster since std::function has some overhead.
You can have a look at std::accumulate or std::reduce, they work fairly similar and are part of the STL ;-)
Happy coding
Ah, thanks! I like that better, I think - good to know about std:: function.
I had come across the STL options but this exercise was specifically to get there myself :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can also add an additional template parameter BinaryOp like this:
See the code. And its also slightly faster since std::function has some overhead.
You can have a look at std::accumulate or std::reduce, they work fairly similar and are part of the STL ;-)
Happy coding
Ah, thanks! I like that better, I think - good to know about std:: function.
I had come across the STL options but this exercise was specifically to get there myself :)