Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
sum1.cpp
Go to the documentation of this file.
1 /*
2 // * This file is part of the gobelijn software.
3  * Gobelijn is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the
5  * Free Software Foundation, either version 3 of the License, or any later
6  * version. Gobelijn is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.
9  * See the GNU General Public License for details. You should have received
10  * a copy of the GNU General Public License along with the software. If not,
11  * see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2012, Jan Broeckhove, CoMP/UA.
14  */
20 // BEGIN_SNIPPET{FullSource}
21 #include "Range.h"
22 #include <list>
23 #include <vector>
24 
25 template <typename C>
26 typename C::value_type sum(C const& c)
27 {
28  typename C::value_type val = typename C::value_type();
29  for (auto it = c.cbegin(); it != c.cend(); ++it) {
30  val += *it;
31  }
32  return val;
33 }
34 
35 int main()
36 {
37  std::vector<int> v;
38  v.push_back(1);
39  v.push_back(2);
40  v.push_back(3);
41  std::cout << make_range(v) << " sum: " << sum(v) << std::endl;
42  std::list<double> l;
43  l.push_back(1);
44  l.push_back(2);
45  l.push_back(3);
46  std::cout << make_range(l) << " sum: " << sum(l) << std::endl;
47  return 0;
48 }
49 // END_SNIPPET{FullSource}
Range (see also boost Range library)
C::value_type sum(C const &c)
Definition: sum1.cpp:26
Range< Iter > make_range(Iter it1, Iter it2)
Definition: Range.h:34
int main()
Definition: sum1.cpp:35