I'll often represent and process polylines like so:
typedef std::vector< Point_t > Polyline_t;
double PolylineLength(const Polyline_t& line)
{
double len = 0.0;
for( size_t i = 0; i < line.size()-1; ++i )
len += (line[i+1]-line[i+0]).length();
return len;
}
The most straightforward conversion to bidirectional iterators that I came up with is:
typedef std::list< Point_t > Polyline_t;
typedef Polyline_t::const_iterator Polyline_t_cit;
double PolylineLength(const Polyline_t& line)
{
double len = 0.0;
Polyline_t_cit last = line.end();
last--;
for( Polyline_t_cit i = line.begin(); i != last; ++i )
{
const Point_t& beg = *i;
const Point_T& end = *(++i);
len += (end - beg).length();
--i;
}
return len;
}
Is there a more succinct way to express this sort of two-at-time processing with iterators?