用反向订单编号制作数组

Make array with reverse order numbers

本文关键字:编号 数组 单编号      更新时间:2023-10-16

我要制作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);而不是。