vậy chắc thiếu ở chỗ swap bên a nó ko swap bên b :V
em có thể tạo 1 mảng c mới với mỗi phần tử là c[i] = (a[i], b[i]) :V
std::vector<std::pair<int, int>> c;
std::transform(begin(a), end(a), begin(b), std::back_inserter(c), [](int x, int y) { return std::make_pair(x, y); });
rồi sort mảng c này :V :V
std::sort(begin(c), end(c), [](auto const& x, auto const& y) { return x.second < y.second; });
rồi quẳng nó vào a, b lại :V :V :V
std::transform(begin(c), end(c), begin(a), [](auto const& x) { return x.first; });
std::transform(begin(c), end(c), begin(b), [](auto const& x) { return x.second; });
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> a = {1, 2, 3, 4, 5};
std::vector<int> b = {2, 3, 4, 8, 7};
std::vector<std::pair<int, int>> c;
std::transform(begin(a), end(a), begin(b), std::back_inserter(c),
[](int x, int y) { return std::make_pair(x, y); });
std::sort(begin(c), end(c),
[](auto const& x, auto const& y) { return x.second < y.second; });
std::transform(begin(c), end(c), begin(a),
[](auto const& x) { return x.first; });
std::transform(begin(c), end(c), begin(b),
[](auto const& x) { return x.second; });
for (int x : b) std::cout << x << " ";
std::cout << "\n";
for (int x : a) std::cout << x << " ";
std::cout << "\n";
}