用反向订单编号制作数组
Make array with reverse order numbers
我要制作3个阵列,一个randnumbers,一个按顺序进行,一个以相反的顺序。我的反向顺序阵列无法正常工作。Size,Randorderarr,Inorderarr和Revorderarr都在我的SortingClass类中。这是我的代码,我该如何修复它以使Revorderarr成为具有反向数字的数组?
SortingClass::SortingClass(int si, int sm, int la){
size=si;
randArr = new int[si];
inOrderArr = new int[si];
revOrderArr = new int[si];
srand(time(NULL));
for(int i=0; i<si; i++){
//srand(time(NULL));
int randnum = rand()%(la-sm+1)+sm;
randArr[i]=randnum;
inOrderArr[i]=(sm+i);
revOrderArr[i]=(la-1-i);
}
}
如果您的目标是创建一个随机数和其他两个数组,这些数组代表数组的排序(上升和下降(版本,则使用C 11,您可以做:
#include <algorithm>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <random>
#include <vector>
void fillRandomArrays(
int arraySize,
int minValue,
int maxValue,
std::vector<int>& unsorted,
std::vector<int>& sortedAscending,
std::vector<int>& unsortedDescending
) {
std::random_device randomDevice;
std::mt19937 mersenneTwister(randomDevice);
std::uniform_int_distribution<int> distribution(minValue, maxValue);
//fill the random unsorted array
std::generate_n(std::back_inserter(unsorted), arraySize, [&]() {
return distribution(mersenneTwister);
});
//fill the sorted ascending array
std::copy(std::begin(unsorted), std::end(unsorted), std::back_inserter(sortedAscending));
std::sort(std::begin(sortedAscending), std::end(sortedAscending), std::less<int>());
//fill the sorted descending array
std::copy(sortedAscending.rbegin(), sortedAscending.rend(), std::back_inserter(sortedDescending));
}
您正在使用错误的 revOrderArr
。
使用revOrderArr[i]=(la-i);
而不是。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- 如何在数组中找到最小编号的索引号?
- 用反向订单编号制作数组
- 数组元素编号
- 如何使用模板使用fibonacci编号创建编译时的模板/数组/矢量
- 从数组中获取唯一编号,完全删除重复项
- 如何创建一个ifstream对象的数组,以及如何使用编号的文本文件填充该数组
- 排列 +ve 和 -ve 编号的数组,顺序不变
- 构建一个程序,将 10 个整数放入数组中并识别重复的数字并输出它们及其位置编号的位置
- 随机编号的数组给出相同的数字
- 如何计算数组条目的条目编号
- 如何使用数组对类对象进行编号