我有一个函数,它接受两个与参数大小相同的向量:
void mysort(std::vector<double>& data, std::vector<unsigned int>& index)
{
// For example :
// The data vector contains : 9.8 1.2 10.5 -4.3
// The index vector contains : 0 1 2 3
// The goal is to obtain for the data : -4.3 1.2 9.8 10.5
// The goal is to obtain for the index : 3 1 0 2
// Using std::sort and minimizing copies
}
如何解决最小化所需副本数量的问题?
一种明显的方法是制作一个向量std::pair<double, unsigned int>
并指定比较器[](std::pair<double, unsigned int> x, std::pair<double, unsigned int> y){return x.first < y.first;}
,然后将结果复制到两个原始向量中,但这不会有效。
注意:函数的签名是固定的,我不能传递std::pair
.