划分有关枢轴C 的数组
Partitioning an array about a pivot C++
我试图将数组分为两个区域S1&S2。枢轴必须是数组的第一个元素。因此,例如:
7 5 9 2 6
7是枢轴。
我想拥有S1区域;小于枢轴
的数字S2区域;比枢轴大的数字。
这样:
5 2 6 7 9
如何以C 语言实现此功能?
您可以使用std::stable_partition
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v{7,5,9,2,6};
std::stable_partition(v.begin(), v.end(), [x=v[0]](int n){return n<x;});
for (int n : v) {
std::cout << n << ' ';
}
std::cout << 'n';
}
输出
5 2 6 7 9
demo
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- 如何在 C++ 中将数组划分为不同的数组
- 一个数组可以划分的最大子数组,使得不同子数组中任意两个元素的 GCD 始终为 1?
- 确定数组是否可以划分为两个子序列,每个子序列的顺序递增
- 划分有关枢轴C 的数组
- 使用特殊规则将数组划分为两个数组
- 简单的数组排序/检查 划分和征服版本
- 用getline在字符串中从文件中读取,然后将数字从int数组中的字符串划分
- 删除每个元素可以将数组中的另一个元素划分
- 使用迭代器将数组划分为大小不等的部分
- 如何将字符串划分为字符数组
- 根据条件划分数组中的元素