【问题标题】:How can I sort an array of double pointers based on the values they point to?如何根据它们指向的值对双指针数组进行排序?
【发布时间】:2010-09-06 05:57:13
【问题描述】:

我正在尝试在 C/C++ 中构建一个函数来对数组进行排序并将每个值替换为其“分数”或排名。它接收一个指向整数数组的双指针数组,并根据取消引用的整数值对双指针进行排序。我已经尝试了很多次让它工作,但无法让它下来。再一次,它必须根据它们指向的值对双指针进行排序。这就是我所拥有的:

void SortArray( int ** pArray, int ArrayLength )
{
  int i, j, flag = 1;     // set flag to 1 to begin initial pass
  int * temp;             // holding variable orig with no *
  for(i = 1; (i <= ArrayLength) && flag; i++)
  {
    flag = 0;
    for (j = 0; j < (ArrayLength -1); j++)
    {
        if (*pArray[j+1] > *pArray[j])    // ascending order simply changes to <
        { 
            temp = &pArray[j];            // swap elements
            pArray[j] = &pArray[j+1];
            pArray[j+1] = &temp;
            flag = 1;                     // indicates that a swap occurred.
        }
    }
  }
}

【问题讨论】:

标签: c++ c arrays pointers reference


【解决方案1】:

您应该考虑使用std::swap() 进行交换。如果你这样做,就这样称呼它:

swap( obj1, obj2 );

而不是:

std::swap( obj1, obj2 );

作为第一个调用语义将允许正确的命名空间查找以找到正确的重载(如果存在)。一定要有:

using namespace std;

或:

using std::swap;

某处。

【讨论】:

    【解决方案2】:

    嗯,我对 STL 没有太多经验。可以举个例子吗?

    此程序创建一个整数向量,对其进行排序并显示结果。

    #include <vector>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        vector<int>; vec;
        vec.push_back(7);
        vec.push_back(5);
        vec.push_back(13);
        sort(vec.begin(), vec.end());
    
        for (vector<int>::size_type i = 0; i < vec.size(); ++i)
        {
            cout << vec[i] << endl;
        }
    }
    

    【讨论】:

      【解决方案3】:

      要完成 Brian Ensink 的帖子,您会发现 STL 充满了惊喜。例如 std::sort 算法:

      #include <iostream>
      #include <vector>
      #include <algorithm>
      
      void printArray(const std::vector<int *> & p_aInt)
      {
         for(std::vector<int *>::size_type i = 0, iMax = p_aInt.size(); i < iMax; ++i)
         {
            std::cout << "i[" << static_cast<int>(i) << "] = " << reinterpret_cast<unsigned     int>(p_aInt[i]) << std::endl ;
         }
      
         std::cout << std::endl ;
      }
      
      
      int main(int argc, char **argv)
      {
         int a = 1 ;
         int b = 2 ;
         int c = 3 ;
         int d = 4 ;
         int e = 5 ;
      
         std::vector<int *> aInt ;
      
         // We fill the vector with variables in an unordered way
         aInt.push_back(&c) ;
         aInt.push_back(&b) ;
         aInt.push_back(&e) ;
         aInt.push_back(&d) ;
         aInt.push_back(&a) ;
      
         printArray(aInt) ; // We see the addresses are NOT ordered
         std::sort(aInt.begin(), aInt.end()) ; // DO THE SORTING
         printArray(aInt) ; // We see the addresses are ORDERED
      
         return EXIT_SUCCESS;
      }
      

      数组的第一次打印将显示无序地址。第二个,在排序之后,将显示有序的地址。在我的编译器上,我们有:

      i[0] = 3216087168
      i[1] = 3216087172
      i[2] = 3216087160
      i[3] = 3216087164
      i[4] = 3216087176
      
      i[0] = 3216087160
      i[1] = 3216087164
      i[2] = 3216087168
      i[3] = 3216087172
      i[4] = 3216087176
      

      看看 STL 的 标头http://www.cplusplus.com/reference/algorithm/ 你会发现很多实用程序。请注意,您还有其他更适合您的容器实现(std::list?std::map?)。

      【讨论】:

      • 实际上他想对所指向的值进行排序。您需要在排序中添加一个 lambda 以取消引用指针,而不是对指针进行排序。检查原始帖子。
      【解决方案4】:

      嘿,这不是作业。

      如果是这样,那么考虑使用 STL 来管理数组和排序。它更易于开发和维护,并且 std::sort 算法比冒泡排序渐进地快。

      【讨论】:

        【解决方案5】:

        你已经接近了。您在交换时引用了数组项的地址,这不是必需的。数组中的项目是指针,这就是需要交换的。

        见下文:

        void SortArray( int ** pArray, int ArrayLength )
        {
            int i, j, flag = 1;    // set flag to 1 to begin initial pass
            int * temp;             // holding variable orig with no *
            for(i = ArrayLength - 1; i > 0 && flag; i--)
            {
                flag = 0;
                for (j = 0; j < i; j++)
                {
                    if (*pArray[j] > *pArray[j+1])      // ascending order simply changes to <
                    { 
                        temp = pArray[j];             // swap elements
                        pArray[j] = pArray[j+1];
                        pArray[j+1] = temp;
                        flag = 1;               // indicates that a swap occurred.
                    }
                }
            }
        }
        

        另外,如果您有兴趣,请查看this lovely blog post on Bubble Sorting(对不起,无耻的插件 :))。希望对你的功课有所帮助;)


        编辑:注意微妙的“优化”,您从数组长度倒数,并且只增加直到内部循环中的“i”。这样可以避免不必要地重新分析已排序的项目。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-21
          • 2014-05-16
          相关资源
          最近更新 更多