【问题标题】:How to sort int** array pointing to the int* array elements如何对指向 int* 数组元素的 int** 数组进行排序
【发布时间】:2013-03-16 18:32:17
【问题描述】:

我有 2 个数组

int n = 10;
int a[n];
int** t = new int*[n];

然后我将ti 元素指向ai 元素 然后我只想对t数组中的指针进行排序 然后我尝试对其进行冒泡排序,但它出错了

do{
    for(int i = 0; i < n -1; i++){
        if(*t[i] > *t[i+1]){
            char* x = t[i];
            t[i] = t[i+1];
            t[i+1] = x;
        }
        n--;
    }
}while(n>1);

【问题讨论】:

  • t 中的指针已经排序,因为 i'th 指向 a 中的第 i 个元素。

标签: c++ arrays sorting pointers int


【解决方案1】:

如果你在 for 循环中递减 n,它不会遍历整个数组,请尝试将它放在 for 循环之外

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多