希望我能得到一些关于我所做的排序方法的建议。
此代码的目的是创建一个 int 指针数组,并按常规 int 数组的内容对该数组中的指针进行排序。然后根据原始 int 数组的位置为不同的变量赋值。
我在这段代码中遇到的奇怪之处在于,据我所知,测试代码不应该影响任何事情......实际上正在影响我的指针的内容。也许值没有改变,但我编写测试代码的方式导致错误。
 //create array
 int c[8] = {3,1,5,7,8,2,6,4};
 //create pointer array
 int *newptr[8];
 for(int k = 0; k<8; k++)
 {
     newptr[k] = &c[k];
 }
//sort pointer array
for(int j = 0; j<8; j++)
{
    for(; j > -1 && *newptr[j] < *newptr[j+1]; j--)
    {
        int *temp = newptr[j+1];
        newptr[j+1] = newptr[j];
        newptr[j] = temp;
    }
}
//set lookuplocation
int lookuplocation;
for(int i = 0; i<8; i++)
{
    cout << *newptr[i];
    if(newptr[i] == &c[0])
    {
        cout << *newptr[i] << endl;
        //If I use endl or \n to test the pointers values I end up with only
        //a part of the correct data. 
        cout << "\nSuccess!\n";
        lookuplocation = 0;
    }
}
//Also for my last test sometimes the first element gets messed up as well
//test arrays
for(int k = 0; k<8; k++)
{
    cout << "Element " << k << ": " << *newptr[k] << endl;
    cout << "Element " << k << ": " << newptr[k] << endl;
}