C++20 sections · 1024 units
Open in Course

Coordinates and Points

Geometric applications

Pairs are perfect for representing 2D coordinates. pair<int, int> or pair<double, double> can store (x, y) points. Example: vector<pair<int, int>> points; points.push_back({3, 4}); The .first is x.second is y.

This convention is widespread in competitive programming. You get free lexicographic comparison: points sort by x first, then by y. If you need a different order (like by distance from origin), use a custom comparator.

For 3D points, use tuple<int, int, int> or define a struct. Structs are better when you have many operations on points (distance, dot product, etc.).