Auxiliary Arithmetic Algorithms
misc_algorithms.hpp
1 #pragma once
2 
3 #include <numeric>
4 
5 #include "traits.hpp"
6 
7 namespace aaa {
8 
18 template<typename InputIterator, typename OutputIterator>
19 void convert(InputIterator first_in, InputIterator last_in, OutputIterator first_out)
20 {
21  auto f = [](value_type_i<InputIterator> x)
22  {
23  return value_type_i<OutputIterator>(x);
24  };
25  std::transform(first_in, last_in, first_out, f);
26 }
27 
31 template<typename Container1, typename Container2>
32 void convert(const Container1& in, Container2& out)
33 {
34  assert(in.size() == out.size());
35  using std::begin;
36  using std::end;
37  return convert(begin(in), end(in), begin(out));
38 }
39 
43 template<typename InputIterator, typename T = value_type_i<InputIterator>>
44 T sum(InputIterator first, InputIterator last, T init = T{})
45 {
46  return std::accumulate(first, last, init);
47 }
48 
52 template<typename Container, typename T = value_type<Container>>
53 T sum(const Container& container, T init = T{})
54 {
55  using std::begin;
56  using std::end;
57  return sum(begin(container), end(container), init);
58 }
59 
62 } // namespace aaa
void convert(const Container1 &in, Container2 &out)
Definition: misc_algorithms.hpp:32
T sum(const Container &container, T init=T{})
Definition: misc_algorithms.hpp:53
Definition: add.hpp:7