Skip to content

Instantly share code, notes, and snippets.

@andrey-malets
Created July 12, 2016 19:38
Show Gist options
  • Save andrey-malets/38ba9ffdca26042bb11a703d12b55526 to your computer and use it in GitHub Desktop.
Save andrey-malets/38ba9ffdca26042bb11a703d12b55526 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
template<class It> using Value = typename std::iterator_traits<It>::value_type;
template<class It> Value<It> sum(It begin, It end) {
Value<It> res = 0;
while (begin != end)
res += *begin++;
return res;
}
int main(void) {
int a[] = {0, 1, 2, 3, 4, 5};
size_t size = sizeof(a) / sizeof(a[0]);
std::cout << sum(a, a + size) << std::endl;
std::vector<int> v(a, a + size);
std::cout << sum(v.begin(), v.end()) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment