-
Notifications
You must be signed in to change notification settings - Fork 86
Improve STL exercise #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve STL exercise #222
Conversation
bernhardmgruber
commented
Aug 12, 2022
- Prefix STL calls with std::
- Add const to variables with are not mutated
- Add lambda based alternative solution
* Prefix STL calls with std:: * Add const to variables with are not mutated * Add lambda based alternative solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might have found a bug, please check.
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T()); | ||
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const T sum = std::reduce(diffs.begin()+1, diffs.end(), T()); | |
const T sumsq = std::reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>()); | |
const T sum = std::reduce(diffs.begin(), diffs.end(), T()); | |
const T sumsq = std::reduce(diffs.begin(), diffs.end(), T(), sumsquare<T>()); |
Why do you skip the first element? Was this meant to be
const T sum = std::reduce(diffs.begin(), diffs.end(), diffs[0]);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is correct, since std::adjacent_difference
only writes differences starting at element 1. So diffs[0]
is just v[0]
and diffs[1]
is the first difference (v[1] - v[0]
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I ignored where those values came from. This is correct, but not very intuitive without any explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With your suggestions (that I committed), this is resolved now I guess?
Co-authored-by: Stephan Hageboeck <stephan.hageboeck@cern.ch>
Co-authored-by: Stephan Hageboeck <stephan.hageboeck@cern.ch>