C++, Sort One Vector Based On Another One(C++,基于另一个向量对一个向量进行排序)
问题描述
我得到的最好的例子是我想根据他们的分数对姓名进行排序.
The best example I've got is that I want to sort Names based on their Score.
因此,如果我将分数排序为 {5, 14, 24, 45},则名称也应根据其分数进行排序.
So if I sort the score to {5, 14, 24, 45}, the names should also be sorted based on their score.
推荐答案
正如其他答案中已经建议的那样:结合每个人的姓名和分数可能是最简单的解决方案.
As already suggested in other answers: Combining the name and the score of each individual is likely the simplest solution.
通常,这可以通过有时称为压缩"操作的方式来实现:将两个向量组合成一对向量 - 以及相应的解压缩".
Generically, this can be achieved with what is sometimes referred to as a "zip" operation: Combining two vectors into a vector of pairs - along with a corresponding "unzip".
一般实现,这可能如下所示:
Implemented generically, this may look as follows:
这篇关于C++,基于另一个向量对一个向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!