小编Yur*_*ury的帖子

在两个范围内按降序对向量进行排序

假设我有一个整数向量:

std::vector<int> indices;
for (int i=0; i<15; i++) indices.push_back(i);
Run Code Online (Sandbox Code Playgroud)

然后我按降序排序:

sort(indices.begin(), indices.end(), [](int first, int second) -> bool{return indices[first] > indices[second];})
for (int i=0; i<15; i++) printf("%i\n", indices[i]);
Run Code Online (Sandbox Code Playgroud)

这会产生以下结果:

14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
Run Code Online (Sandbox Code Playgroud)

现在我想将数字 3、4、5 和 6 移到最后,并保持它们的降序(最好不必sort第二次使用)。即,这是我想要的:

14
13
12
11
10
9
8
7
2
1
0
6
5
4
3
Run Code Online (Sandbox Code Playgroud)

我应该如何修改 的比较功能std::sort来实现这一点?

c++ sorting vector stdvector c++11

14
推荐指数
2
解决办法
414
查看次数

标签 统计

c++ ×1

c++11 ×1

sorting ×1

stdvector ×1

vector ×1