我在使用排序运算符时遇到了问题,因为我只需要对对中的第一个元素进行排序。代码很简单但不起作用:
运算符定义于:
struct sort_pred {
bool operator()(const CromosomaIndex &left, const CromosomaIndex &right) {
return left.first < right.first;
}
};
类型是
typedef std::pair<double,int> CromosomaIndex;
我正在尝试像这样对数组进行排序:
CromosomaIndex nuevo[2];
nuevo[0].first = 0.01;
nuevo[0].second = 0;
nuevo[1].first = 0.009;
nuevo[1].second = 1;
int elements = sizeof(nuevo) / sizeof(nuevo[0]);
sort(nuevo, nuevo+ elements, sort_pred());
但问题是这是对第一个和第二个元素进行排序,而我只想对第一个元素进行排序并保持第二个元素不变。 有什么想法吗?
最佳答案
如果您希望结果取决于原始顺序,请使用 std::stable_sort
.
关于c++ - 排序运算符在 C++ 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12127197/