Practical Examples
Example 1: Student Grade Management
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
class StudentGradeManager {
private:
std::vector<std::pair<std::string, double>> students;
public:
void addStudent(std::string name, double grade) {
students.push_back({name, grade});
}
void sortByGrade() {
std::sort(students.begin(), students.end(),
[](const auto& a, const auto& b) {
return a.second > b.second;
});
}
void displayTopStudents(int count) {
for (int i = 0; i < std::min(count, (int)students.size()); ++i) {
std::cout << students[i].first
<< ": "
<< students[i].second
<< std::endl;
}
}
};
int main() {
StudentGradeManager manager;
manager.addStudent("Alice", 95.5);
manager.addStudent("Bob", 87.3);
manager.addStudent("Charlie", 92.1);
manager.sortByGrade();
manager.displayTopStudents(2);
return 0;
}
Example 2: Coordinate System Manipulation
#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
class CoordinateSystem {
private:
std::vector<std::pair<int, int>> points;
public:
void addPoint(int x, int y) {
points.push_back({x, y});
}
double calculateDistance(const std::pair<int, int>& p1,
const std::pair<int, int>& p2) {
int dx = p1.first - p2.first;
int dy = p1.second - p2.second;
return std::sqrt(dx * dx + dy * dy);
}
std::pair<int, int> findClosestPoint(int x, int y) {
std::pair<int, int> target = {x, y};
return *std::min_element(points.begin(), points.end(),
[&](const auto& p1, const auto& p2) {
return calculateDistance(target, p1) <
calculateDistance(target, p2);
});
}
};
int main() {
CoordinateSystem coords;
coords.addPoint(0, 0);
coords.addPoint(3, 4);
coords.addPoint(5, 12);
auto closest = coords.findClosestPoint(2, 3);
std::cout << "Closest Point: ("
<< closest.first << ", "
<< closest.second << ")" << std::endl;
return 0;
}
Example 3: Key-Value Pair Processing
#include <iostream>
#include <map>
#include <utility>
#include <string>
class InventoryManager {
private:
std::map<std::string, std::pair<int, double>> inventory;
public:
void addProduct(const std::string& name, int quantity, double price) {
inventory[name] = {quantity, price};
}
double calculateTotalValue() {
double total = 0.0;
for (const auto& [name, details] : inventory) {
total += details.first * details.second;
}
return total;
}
void displayInventorySummary() {
std::cout << "Inventory Summary:\n";
for (const auto& [name, details] : inventory) {
std::cout << name
<< " - Qty: " << details.first
<< ", Price: $" << details.second
<< std::endl;
}
}
};
int main() {
InventoryManager manager;
manager.addProduct("Laptop", 10, 1000.0);
manager.addProduct("Smartphone", 20, 500.0);
manager.displayInventorySummary();
std::cout << "Total Inventory Value: $"
<< manager.calculateTotalValue()
<< std::endl;
return 0;
}
Practical Use Cases
graph TD
A[Pair Use Cases] --> B[Data Management]
A --> C[Algorithm Implementation]
A --> D[Complex Data Structures]
A --> E[Performance Optimization]
Key Takeaways
Scenario |
Pair Utility |
Data Storage |
Compact representation of two related values |
Function Returns |
Multiple return values |
Sorting |
Easy comparison and sorting |
Mapping |
Key-value pair representations |
By exploring these practical examples, you'll gain insights into the versatility of pairs in C++. LabEx recommends practicing these techniques to enhance your programming skills and problem-solving abilities.