Skip to main content
added 21 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I would just use adjacent_difference instd::adjacent_difference from the numeric<numeric> library. It's designed to accept custom binary ops such as plus<>plus<> and multiplies<>multiplies<> from the functional<functional> library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin == end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin == end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}

I would just use std::adjacent_difference from the <numeric> library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the <functional> library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin == end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}
edited body
Source Link
Snowhawk
  • 6.4k
  • 1
  • 18
  • 36

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin >=== end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin >= end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin == end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}
Code cleanup.
Source Link
Snowhawk
  • 6.4k
  • 1
  • 18
  • 36

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) { 
    if (begin ==>= end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
    ++begin;    auto next = *begin;
    ++result;    *++result = last + next;
    ...    last = std::move(next);
    }

    return result;
}

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) { 
    if (begin == end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    *result = last;
    ++begin;
    ++result;
    ...

I would just use adjacent_difference in the numeric library. It's designed to accept custom binary ops such as plus<> and multiplies<> from the functional library.

std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());

If you are going to use your own adjacent sum, please include handling for empty/single element lists.

template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
    if (begin >= end)    // Corbin
        return result;

    auto last = *begin;  // Loki
    for (*result = last; ++begin != end;) { // Me
        auto next = *begin;
        *++result = last + next;
        last = std::move(next);
    }

    return result;
}
Source Link
Snowhawk
  • 6.4k
  • 1
  • 18
  • 36
Loading