Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自排序数组';C+中的s排序函数+;自定义类_C++_Sorting_Variable Assignment - Fatal编程技术网

C++ 自排序数组';C+中的s排序函数+;自定义类

C++ 自排序数组';C+中的s排序函数+;自定义类,c++,sorting,variable-assignment,C++,Sorting,Variable Assignment,因此,我尝试创建一个数组类,它允许您添加一个数字,add函数将该数字放入一个数组中,其位置符合以下条件:数组的行和列必须按升序排列。所以 1.2 34 很好,但是 13 4.2 失败,如4>2和3>2,但 13 2 4 可以接受 赋值是模糊的,只要满足约束条件,它是否使数组像第一个或第三个数组并不重要。我通过简单地写出一个案例列表并尝试处理每一个案例来实现我的add功能。当数组初始化时,它会在每个点中放置整数_MAX,以便于放置和评估。我不确定这是求值的顺序还是什么,但有时它会将我添加的数字放在

因此,我尝试创建一个数组类,它允许您添加一个数字,add函数将该数字放入一个数组中,其位置符合以下条件:数组的行和列必须按升序排列。所以

1.2
34

很好,但是

13
4.2

失败,如4>2和3>2,但

13
2 4

可以接受

赋值是模糊的,只要满足约束条件,它是否使数组像第一个或第三个数组并不重要。我通过简单地写出一个案例列表并尝试处理每一个案例来实现我的add功能。当数组初始化时,它会在每个点中放置整数_MAX,以便于放置和评估。我不确定这是求值的顺序还是什么,但有时它会将我添加的数字放在整数_MAX下,或者其他不符合顺序的地方。我在这方面已经做了一段时间了,我不太愿意寻求帮助,但我想如果有一双新的眼睛,可能会更容易。我将为函数和类添加代码,我不知道是否应该/如何包括从属类,以允许其他人编译代码?我是SO的新成员,有点生病,所以请容忍我,我会提供任何需要帮助的信息。谢谢大家!

下面是add()函数本身:

void add(int i) {

    //THIS NEEDS TO BE FIXED ITS GOING TO THE WRONG PLACES

    if (matrix[row - 1][col - 1] != INT_MAX){ //if the last element in the VNT is full 
        cout<<"VNT is full!"<<endl; 
    }
    else {
        matrix[row - 1][col - 1] = i;
        //cout << "matrix["<<row-1<<"]["<<col-1<<"] = " <<matrix[row - 1][col - 1]<<endl;
        int r = row - 1;
        int c = col - 1;
        while (true) {
            if (r == 0 && c == 0) //no neighbor left, no neighbor above, correct position
                break;
            else if (c == 0) { //no neighbor left
                if (matrix[c][r-1] > i) { //if above is larger, swap
                    swap (r, c, r-1, c);
                    r--; //decrement row to go through stability check again
                }
                else        //above is smaller, break
                    break;
            }
            else if (r == 0) { //no neighbor above
                if (matrix[c-1][r] > i) { //if left is larger, swap
                    swap (r, c, r, c-1);
                    c--; //decrement column to go through stability check again
                }
                else        //left is smaller, break
                    break;
            }

            else if (matrix[r][c-1] < i && matrix[r-1][c] < i) //left and above are both smaller, right position
                break;

            else if (matrix[r][c-1] > i && matrix[r-1][c] > i) { //both left and above are potential candidates for switch
                if (matrix[r][c-1] >= matrix[c][r-1]) { //if left candidate is larger than top candidate, swap with that to preserve column
                    swap (r, c, r, c-1);
                    cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r<<"]["<<c-1<<"]"<<endl;
                    c--; //decrement column to go through stability check again
                }
                else { //otherwise swap with neighbor above
                    swap (r, c, r-1, c);
                    cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r-1<<"]["<<c<<"]"<<endl;
                    r--; //decrement row to go through stability check again
                }
            }
            else if (matrix[r][c-1] > i) { //only left neighbor is larger, swap left
                swap (r, c, r, c-1);
                cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r<<"]["<<c-1<<"]"<<endl;
                r--;
            }
            else if (matrix[r-1][c] > i) { //only the above neighbor is larger, switch with that
                swap (r, c, r-1, c);
                cout << "Swapping a["<<r<<"]["<<c<<"] with a["<<r-1<<"]["<<c<<"]"<<endl;
                c--;
            }

        } 
    }
}
void添加(int i){
//这个问题需要解决,因为它去错了地方
if(矩阵[row-1][col-1]!=INT_MAX){//如果VNT中的最后一个元素已满

cout像处理一维数组一样处理数组可能是最简单的方法,并使整个数组保持有序

然后,当您显示它时,您可以按列或按行打印它,并且它保证满足您的约束。在这两种情况中,按行打印几乎肯定会(相当)容易


当然,对于任何实际使用,您无疑不希望将数据设为全局数据,也不希望有像
insert
print
这样的函数隐式地在该全局数据上运行,但我希望基本思想仍然能够实现。

看看我在下面写的代码片段

我同意Jerry的观点,它更容易使用和排序一维数组,并将其投影到您需要的形式。我使用了与Jerry相同的排序例程,只是这将在您所需宽度的方阵上显示您的数字

#define DIMENSION_SIZE 10

int matrix[DIMENSION_SIZE+1][DIMENSION_SIZE+1];

std::vector<int> data;

void insert(int d) { 
    auto pos = std::upper_bound(data.begin(), data.end(), d);
    data.insert(pos, d);
}

int main()
{
    for (int ctr=0;ctr<DIMENSION_SIZE*DIMENSION_SIZE;++ctr) {
        insert(rand());
    }

    int level = 0;
    int levelsub = 0;

    for (int ctr=0;ctr<DIMENSION_SIZE*DIMENSION_SIZE;++ctr) {
        matrix[level-levelsub][levelsub] = data.at(ctr);

        if (++levelsub >  (level >= DIMENSION_SIZE ? DIMENSION_SIZE -1 : level)) {
            ++level;
            levelsub= 0;
            if (level >= DIMENSION_SIZE) {
                levelsub+=level - DIMENSION_SIZE+1;
            }
        }
    }

    for (int ctr_y=0;ctr_y<DIMENSION_SIZE;++ctr_y) { 
        for (int ctr_x=0;ctr_x<DIMENSION_SIZE;++ctr_x) {
            std::cout << matrix[ctr_x][ctr_y] << "\t";
        }
        std::cout << "\n";
    }

    return 0;
}

输出应该总是水平排列,垂直排列(甚至是对角排列!)

谢谢你的洞察力!我甚至没有这样想,但这比我试图将其转换成的无数案例要容易得多。
std::vector<int> data;

void insert(int d) { 
    auto pos = std::upper_bound(data.begin(), data.end(), d);
    data.insert(pos, d);
}

void print(int columns) { 
    for (int i=0; i<data.size(); i++) {
       if (i % columns == 0)
           std::cout << "\n";
       std::cout << data[i];
    }
}
int main(){
    insert(4);
    insert(2);
    print(2);
    std::cout << "\n";
    insert(3);
    insert(1);
    print(2);
}
24

12
34
#define DIMENSION_SIZE 10

int matrix[DIMENSION_SIZE+1][DIMENSION_SIZE+1];

std::vector<int> data;

void insert(int d) { 
    auto pos = std::upper_bound(data.begin(), data.end(), d);
    data.insert(pos, d);
}

int main()
{
    for (int ctr=0;ctr<DIMENSION_SIZE*DIMENSION_SIZE;++ctr) {
        insert(rand());
    }

    int level = 0;
    int levelsub = 0;

    for (int ctr=0;ctr<DIMENSION_SIZE*DIMENSION_SIZE;++ctr) {
        matrix[level-levelsub][levelsub] = data.at(ctr);

        if (++levelsub >  (level >= DIMENSION_SIZE ? DIMENSION_SIZE -1 : level)) {
            ++level;
            levelsub= 0;
            if (level >= DIMENSION_SIZE) {
                levelsub+=level - DIMENSION_SIZE+1;
            }
        }
    }

    for (int ctr_y=0;ctr_y<DIMENSION_SIZE;++ctr_y) { 
        for (int ctr_x=0;ctr_x<DIMENSION_SIZE;++ctr_x) {
            std::cout << matrix[ctr_x][ctr_y] << "\t";
        }
        std::cout << "\n";
    }

    return 0;
}
41      153     292     1842    3035    4966    6729    9741    12316   15350

288     491     1869    3548    5436    6868    9894    12382   15724   18467

778     2082    3902    5447    7376    9961    12623   15890   18716   19954

2995    4664    5537    7711    11323   12859   16118   18756   20037   23805

4827    5705    8723    11478   13931   16541   19169   21538   23811   25547

6334    8942    11538   14604   16827   19264   21726   24084   25667   27446

9040    11840   14771   16944   19629   22190   24370   26299   27529   28703

11942   15006   17035   19718   22648   24393   26308   27644   29358   31101

15141   17421   19895   22929   24464   26500   28145   30106   31322   32439

17673   19912   23281   24626   26962   28253   30333   32391   32662   32757