Pairs, Tuples, and Structs
Related values should be stored together so sorting or passing one record does not separate its fields. std::pair groups two values, std::tuple groups a fixed list of potentially different types, and a struct gives fields meaningful names. We read a point, compute its squared distance from the origin, and package the point’s ID, coordinates and distance in these three ways.
Prerequisites: Functions; arrays; Data Types and Operators.
Build the idea
1. Use pair for a small natural association
std::pair<long long,long long> point{x,y} stores coordinates as first and second. The two types need not match. Pairs compare lexicographically by default: first is compared before second. That is useful for sorting coordinates or (distance,vertex) entries, but it is not automatically geometric distance order. Include <utility> for pair.
2. Use tuple for a fixed collection
A tuple can store (id,x,y), with fields accessed by std::get<0>, std::get<1> and so on. The index is a compile-time template argument, not an arbitrary runtime variable. A tuple’s meaning depends on remembering each position. If a record acquires many fields, named struct members are usually easier to read than a long list of numeric tuple positions.
3. Name fields with a struct
Point contains id,x,y. Point p{id,x,y} initializes fields in declaration order, and p.x accesses one field. A struct defines a type, while each object holds its own values. The terminating semicolon after the struct definition is required. Keep arithmetic in a suitable wide type: squared coordinates can exceed an int even when each coordinate itself fits.
4. Unpack using structured bindings
C++17 allows auto [a,b]=point and auto [savedId,savedX,savedY]=record. These bindings copy values by default. auto& bindings can refer to the original object, but use them only when mutation or avoiding copies is intended. Structured bindings improve readability without changing the underlying algorithm or granting extra fields.
5. Work the point example
Input ID 7 at coordinates (3,4) produces x²+y²=9+16=25. A tuple preserves ID 7 alongside 3 and 4; the struct retains the same coordinate names. The program outputs the tuple’s three fields, then squared distance and the pair’s second coordinate. No square root is needed when comparing nonnegative distances from the same origin: squared order is sufficient.
C++17 implementation
This is a complete program. Compile with C++17 and enter the input described below.
#include<iostream>
#include<utility>
#include<tuple>
struct Point{
int id;
long long x,y;
};
int main(){
int id;
long long x,y;
std::cin>>id>>x>>y;
std::pair<long long,long long>coordinates{x,y};
std::tuple<int,long long,long long>record{id,x,y};
auto[savedId,savedX,savedY]=record; // C++17 structured binding (copies).
Point point{id,x,y};
long long squared=point.x*point.x+point.y*point.y;
std::cout<<savedId<<' '<<savedX<<' '<<savedY<<'\n';
std::cout<<squared<<' '<<coordinates.second<<'\n';
}
Complexity analysis
O(1) time and space: the record has a fixed number of machine-sized fields. A container holding n records would need O(n) storage, and sorting such records has a separate complexity. With |x|,|y|≤10^9, the sum of squares is at most 2×10^18 and fits long long on standard 64-bit contest environments.
Example input and output
Read int id and coordinates x y with |x|,|y|≤10^9. Print the stored tuple, then squared distance and the second coordinate.
Input
7 3 4
Output
7 3 4 25 4
The distance squared is 3×3+4×4=25; pair.second is 4.
Practical applications
Use pairs for compact two-field objects such as (value,index), tuples for small fixed states such as (distance,vertex,flag), and structs when names clarify a domain object. Grouping fields prevents correspondence bugs during sorting and is a preparation for graph edges and priority-queue states.
Check your understanding
What is a major advantage of a struct over a long tuple?
Put it into practice
Store several points in a vector of structs and print the closest one.
Show a hint
Compare squared distances and define a tie rule for equal distances.