POV-Ray : Newsgroups : povray.off-topic : A comparison : Re: A comparison Server Time
10 Oct 2024 23:19:08 EDT (-0400)
  Re: A comparison  
From: Warp
Date: 19 Mar 2008 16:21:16
Message: <47e183cb@news.povray.org>
Invisible <voi### [at] devnull> wrote:
>    sum [] = 0
>    sum (x:xs) = x + sum xs

  Don't you love it how complicated something like this looks in C++:

template<typename Iterator>
typename std::iterator_traits<Iterator>::value_type
sum(Iterator begin, Iterator end)
{
    typedef typename std::iterator_traits<Iterator>::value_type t;
    t result = t();
    while(begin != end) result += *begin++;
    return result;
}

  Its advantage, though, is that it works with *any* container type
as long as it has sequential iterators, and *any* value type, as long
as it supports the + operator. So you can do for example like this:

  int table[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  int result = sum(table, table+8);

or like this:

  std::string table[] = { "a", "b", "cde", "f" };
  std::string result = sum(table, table+8);

or like this:

  std::vector<double> table(100);
  // init the vector
  double result = sum(table.begin(), table.end());

or like this:

  std::list<std::complex<double> > l;
  // init the list
  std::complex<double> result = sum(l.begin(), l.end());

or like this:

  std::set<std::string> s;
  // init the set
  std::string result = sum(s.begin(), s.end());

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.