-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointcmp.cpp
More file actions
40 lines (32 loc) · 814 Bytes
/
pointcmp.cpp
File metadata and controls
40 lines (32 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cmath>
#include <map>
#include <iostream>
struct Point{double x, y;};
struct PointCmp
{
bool operator()(const Point* lhs, const Point* rhs) const
{
return lhs->x > rhs->x;
}
};
int main()
{
Point points[3] = {{2, 0}, {1, 0}, {3, 0}};
std::map<Point*, double, PointCmp> mag({
{points, 2},
{points+ 1, 1},
{points + 2, 3}
});
for(auto it = mag.begin(); it != mag.end(); ++it)
{
auto cur = it->first;
cur->y = mag[cur];
}
for(auto it = mag.begin(); it != mag.end(); ++it)
{
auto cur = it->first;
mag[cur] = std::hypot(cur->x, cur->y);
std::cout << "The magnitude of (" << cur->x << ", " << cur->y << ") is ";
std::cout << it->second << std::endl;
}
}