how to use iterator in c++?
- by tsubasa
I'm trying to calculate distance between 2 points.
The 2 points I stored in a vector in c++: (0,0) and (1,1).
I'm supposed to get results as
0
1.4
1.4
0
but the actual result that I got is
0
1
-1
0
I think there's something wrong with the way I use iterator in vector.
Could somebody help? I posted the code below.
typedef struct point {
float x;
float y;
} point;
float distance(point *p1, point *p2)
{
return sqrt((p1->x - p2->x)*(p1->x - p2->x) +
(p1->y - p2->y)*(p1->y - p2->y));
}
int main()
{
vector <point> po;
point p1; p1.x=0; p1.y=0;
point p2; p2.x=1; p2.y=1;
po.push_back(p1);
po.push_back(p2);
vector <point>::iterator ii;
vector <point>::iterator jj;
for (ii=po.begin(); ii!=po.end(); ii++)
{
for (jj=po.begin(); jj!=po.end(); jj++)
{
cout<<distance(ii,jj)<<" ";
}
}
return 0;
}