#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
template<typename It>
void replace(
It beg,
It end,
typename iterator_traits<It>::value_type const& oldval,
typename iterator_traits<It>::value_type const& newval)
{
while ((beg = find(beg, end, oldval)) != end)
{
*beg = newval;
++beg;
}
}
template<typename T>
void rearrange_vector(vector<int> nInd, vector<T> &v)
{
size_t indx;
for (size_t ie(v.size()), i(0); i < ie; ++i)
{
auto it = find(nInd.begin(), nInd.end(), i);
if (nInd.end() != it)
{
indx = distance(nInd.begin(), it);
swap(v.at(i), v.at(indx));
replace(nInd.begin(), nInd.end(), indx, i);
}
}
}
int main()
{
vector<string> firstnames = { "Bjarne", "Alexander", "Dennis", "James", "James" };
vector<string> lastnames = { "Stroustrup", "Stepanov", "Ritchie", "Coplien", "Gosling" };
vector<int> indices(firstnames.size());
iota(indices.begin(), indices.end(), 0);
sort(indices.begin(), indices.end(), [&](int i1, int i2){
return firstnames[i1] < firstnames[i2];
});
rearrange_vector(indices, firstnames);
rearrange_vector(indices, lastnames);
for (size_t i(0), ie(firstnames.size()); i < ie; ++i)
{
cout << firstnames[i] << " " << lastnames[i] << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxudW1lcmljPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnRlbXBsYXRlPHR5cGVuYW1lIEl0Pgp2b2lkIHJlcGxhY2UoCglJdCBiZWcsIAoJSXQgZW5kLCAKCXR5cGVuYW1lIGl0ZXJhdG9yX3RyYWl0czxJdD46OnZhbHVlX3R5cGUgY29uc3QmIG9sZHZhbCwgCgl0eXBlbmFtZSBpdGVyYXRvcl90cmFpdHM8SXQ+Ojp2YWx1ZV90eXBlIGNvbnN0JiBuZXd2YWwpCnsKCX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