在C ++中对指针数组进行排序 [英] Sorting Array of Pointers in C++

查看:125
本文介绍了在C ++中对指针数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望我可以从我的排序方法中获得一些建议.

Hoping I can get a little advice on a sorting method I made.

此代码的目的是创建一个int指针数组,并根据常规int数组的内容对该数组中的指针进行排序.然后根据原始int数组的位置为其他变量分配值.

The purpose of this code is to create a int pointer array and sort the pointers in that array by the contents of regular int array. Then assign values for a different variable based on the location of the original int array.

我在这段代码中遇到的奇怪之处是,测试代码不会影响到我所知道的任何东西……实际上是在影响我的指针的内容.也许这些值没有改变,但是我编写测试代码的方式却引起了错误.

The strangeness I am experiencing with this code is that the test code which shouldn't effect anything as far as I know... IS actually effecting the contents of my pointers. Perhaps the values aren't changing but the way I'm writing the test code is causing errors.

 //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;
}

推荐答案

我认为某人实际上可能需要以一种理智的方式对指针数组进行排序:

I figured someone might actually need to sort an array of pointers in a sane way:

#include <iostream>
#include <array>
#include <algorithm>

int main() {
    std::array<int, 8> arr { 3, 5, 4, 1, 2, 7, 6, 8 };
    std::array<int*, 8> p_arr;

    for (unsigned i = 0; i < 8; ++i) {
        p_arr[i] = &arr[i];
    }

    std::sort(p_arr.begin(), p_arr.end(), [](int* a, int* b) { return *a < *b; });

    for (auto i : p_arr) 
        std::cout << *i;
}

丑陋的中间循环完全可以替换为超过 zip pped范围的范围,但是我现在没有自己的带有引用语义的实现,而且我懒于检查提升一个. 1

The ugly middle loop is totally replace'able by range for over zippped range, but I don't have my own implementation with reference semantics right now, and I am too lazy to check the Boost one.1

这是Coliru上的实时样本.

此外,因为我认为我们应该一遍又一遍地重复这一过程,直到新手理解为止:

Also, because I think we should repeat this over and over until newbies understand it:

  • 不要重新发明分类轮(除非它是玩具实现方式)
  • 请尽量避免在C ++中使用指针.

1 这对于确保两个范围(在这种情况下为两个数组)具有相同的长度实际上很重要.不同的压缩约定要么要求范围的长度相同(否则会崩溃或抛出),或者如果其中一个范围太短则填充空白数据.在如此简单的程序中看似显而易见,但在实际代码中要小心.

1This is actually important in order to make sure both ranges (in this case two arrays) have the same length. Different zipping conventions either require the ranges to be of the same length (crashing or throwing otherwise) or fill in the empty data should one of the ranges be too short. While seemingly obvious in such a simple program, be careful in real-world code.

这篇关于在C ++中对指针数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆