Subject: CGAL users discussion list
List archive
Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points
Chronological Thread
- From: "Laurent Rineau (GeometryFactory)" <>
- To:
- Subject: Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points
- Date: Tue, 5 Oct 2010 22:53:52 +0200
- Organization: GeometryFactory
Le mardi 05 octobre 2010 22:42:07, Benoît Presles a écrit :
> Le 5 oct. 10 à 15:14, Laurent Rineau (GeometryFactory) a écrit :
> > On Tuesday 05 October 2010 14:59:23 Benoît Presles wrote:
> >> Hello everybody,
> >>
> >> I am looking for a nice way to copy my data: double* X, double* Y
> >> to a
> >> list of Point_2.
> >> Here is my code:
> >> //My data (example)
> >> double X[]={1,2,3,5,6};
> >> double Y[]={-10,7,5,9,5};
> >> //My output:
> >> std::list<Point_2> points;
> >> //for loop
> >> for(int i=0; i<5; ++i)
> >> {
> >>
> >> //std::cout << X[i] << " " << Y[i] << std::endl;
> >> points.push_back(Point_2(X[i], Y[i]));
> >>
> >> }
> >>
> >> As one can notice, I do a "for loop" to copy my data. It works but
> >> I am
> >> looking for a better way. Indeed, when my array X (or Y) is very big,
> >> the code is a bit slow.
> >
> > You should use std::vector instead of std::list, and reserve the
> >
> > memory:
> > points.reserve(n);
> >
> > where n is the size of your arrays.
>
> I am going to try but in fact I was wondering if there is a way to do
> it without "for loop".
> Maybe with the "std::copy" function ?
You can use boost::zip_iterator to transform X and Y to an iterator whose
value type is a boost::tuple<double, double>, and then use a transform
iterator to transform that iterator to an iterator whose value type is
Point_2. Then you could use std::copy.
But... well, in your case a "for loop" is the most efficient way, as soon as
you allocate the memory before the loop.
--
Laurent Rineau, PhD
Release Manager of the CGAL Project http://www.cgal.org/
R&D Engineer at GeometryFactory http://www.geometryfactory.com/
- [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Benoît Presles, 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Laurent Rineau (GeometryFactory), 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Benoît Presles, 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Laurent Rineau (GeometryFactory), 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Benoît Presles, 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Laurent Rineau (GeometryFactory), 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Benoît Presles, 10/05/2010
- Re: [cgal-discuss] How to copy double* X double* Y to std::list<Point_2> points, Laurent Rineau (GeometryFactory), 10/05/2010
Archive powered by MHonArc 2.6.16.