Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::reverse on MFC CArray

Tags:

c++

stl

mfc

I have an array of points like this:

CArray<CPoint,CPoint> points;

And I need to reverse the order of the points. I've tried this method:

std::reverse( &points[0], &points[0] + points.GetSize() );

And it works. Then I've tried this other method:

std::reverse( &points[0], &points[points.GetUpperBound()] );

But it doesn't work: the last item is not ordered correctly. Why?

like image 425
IFeelGood Avatar asked Feb 11 '23 01:02

IFeelGood


1 Answers

That is because STL algorithms take ranges in the form [b, e) (that is, e exclusive), whereas the function you used returns the position of the last actual last element.


It should be further noted that your second form is even more problematic in the case where the array is empty. The function, according to the documentation, returns -1 in this case. BOOM!

like image 60
Ami Tavory Avatar answered Feb 13 '23 04:02

Ami Tavory