Skip to content

Instantly share code, notes, and snippets.

@andrey-malets
Created July 12, 2016 19:47
Show Gist options
  • Save andrey-malets/bbf4a90f99f0d0600270c32e8ac98d6b to your computer and use it in GitHub Desktop.
Save andrey-malets/bbf4a90f99f0d0600270c32e8ac98d6b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
template<class It> typename It::value_type sum(It begin, It end) {
typename It::value_type 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