Pair - Store Two Values Together
C++
Medium
3 views
Problem Description
Store two related values as single unit.
Real Life: Storing coordinates (x,y), name-age pairs.
Step-by-Step Logic:
1. Create pair using make_pair()
2. Access first element using .first
3. Access second element using .second
4. Can store in vector or other containers
Official Solution
void stl_q10_pair() {
pair<string, int> student = make_pair("Raj", 20);
cout << "Name: " << student.first << endl;
cout << "Age: " << student.second << endl;
// Vector of pairs
vector<pair<string, int>> students;
students.push_back(make_pair("Priya", 22));
students.push_back(make_pair("Amit", 21));
for(auto s : students) {
cout << s.first << " - " << s.second << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!