#include <iostream>
#include <algorithm>
#include <vector>
template <typename T>
void dualSort(std::vector<T>& keys, std::vector<T>& values) {
// Step 1: Create a vector of pairs, where each pair consists of a key-value pair.
std::vector<std::pair<T, T>> pairs;
for (size_t i = 0; i < keys.size(); ++i) {
pairs.push_back(std::make_pair(keys[i], values[i]));
}
// Step 2: Sort the vector of pairs based on the keys.
std::sort(pairs.begin(), pairs.end(), [](const auto& left, const auto& right) {
return left.first < right.first;
});
// Step 3: Update the original vectors with the sorted key-value pairs.
for (size_t i = 0; i < pairs.size(); ++i) {
keys[i] = pairs[i].first;
values[i] = pairs[i].second;
}
}
int main() {
// Step 4: Create two vectors, one for keys and one for values.
std::vector<int> keys = {4, 2, 7, 1, 5};
std::vector<int> values = {40, 20, 70, 10, 50};
// Step 5: Call the dualSort function to sort both vectors based on keys.
dualSort(keys, values);
// Step 6: Display the sorted key-value pairs.
std::cout << "Sorted key-value pairs:\n";
for (size_t i = 0; i < keys.size(); ++i) {
std::cout << keys[i] << ": " << values[i] << "\n";
}
return 0;
}