Created
July 12, 2016 19:38
-
-
Save andrey-malets/38ba9ffdca26042bb11a703d12b55526 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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