【问题标题】:Bubble Sorting Function in C++C++中的冒泡排序函数
【发布时间】:2015-10-15 20:52:18
【问题描述】:

不允许引用数组。我知道在 C++ 中这是非法的。 但是,还有其他方法可以做到这一点吗? 我确定有,但我想不通。

#include <iostream>
using namespace std;

#define UBound(n)   sizeof(n) / sizeof(n[0]);

void SortArray(int & Arr[]) {
    int ArrayLength;
    ArrayLength = UBound(Arr);
    int Min, Temp;
    for (int i = 0; i = ArrayLength; i++) {
        for (int j = 0; j = ArrayLength; j++) {
            if (Arr[i+1] < Arr[i]){
                Temp = Arr[i+1];
                Arr[i+1] = Arr[i];
                Arr[i] = Temp;
                }
        }
    }
} 
void main() {
    int numArray[9] = { 9, 7, 6, 8, 4, 5, 3, 2, 1 };
    SortArray(numArray);
} 

最终功能:

 template < size_t I >
    void SortArray(int(&Arr)[I]) {
        int Min, Temp;
         for (int i = 0; i < I - 1; i++) {
            for (int j = 0; j < I - 1; j++) {
                if (Arr[j+1] < Arr[j]){
                    Temp = Arr[j+1];
                    Arr[j+1] = Arr[j];
                    Arr[j] = Temp;
                }
            }
        } 
    } 

感谢大家的回答。

【问题讨论】:

  • 为什么你不使用 void SortArray(int *Arr) ?
  • 另一种方法来做什么?您要解决的问题是什么?目前尚不清楚引用数组与您显示的代码之间的关系。
  • main 必须在 c++ 中返回 int
  • 您的问题实际上很清楚,但标题具有误导性并且大多数代码无关紧要。您可能应该将您的问题简化为“参考数组”问题
  • 你应该阅读它,如果你的老师正在做真正的 C++,他们应该告诉你。它基本上是一个动态数组,您可以向其中添加元素,删除它们等。它可以作为引用传递(因为它是一个常规的 C++ 对象),这是一种更方便的隐藏指针。这样做的 C 方式是传递指向第一个元素的指针和数组中元素的总数。

标签: c++ bubble-sort


【解决方案1】:

您可以使用函数模板(以及对数组的引用,而不是引用数组(注意括号)):

template<size_t ArrayLength>
void SortArray(int (&Arr)[ArrayLength]) {
    ...
}

【讨论】:

    【解决方案2】:

    您的代码存在多个问题。让我列出来

    • using namespace std; -- 永远不要这样做。
    • #define UBound -- 首先,你永远不需要这个宏。其次,这个定义是错误的。
    • SortArray 正在尝试接收引用数组。它应该是template &lt;size_t N&gt; void SortArray(int (&amp;Arr)[N])——接收对数组的引用;或void SortArray(int Arr[], size_t len)

    【讨论】:

    • 那么为什么不“使用命名空间 std;”呢?
    • @AliPadida 我想你的老师告诉你这样做,在课堂上它可能没问题。基本上,这允许您使用给定命名空间中的所有名称而不限定它们,因此cout 而不是std::cout。但是,由于可能的名称冲突,它通常不受欢迎(想象一下,您有两个内部具有相同名称的名称空间;如果您对它们都使用using,那么如果使用其中一个,您的代码将无法编译) .更多详情见stackoverflow.com/questions/1452721/…
    【解决方案3】:

    有,但不一定是你应该使用的:

    template < size_t I >
    void SortArray(int (&Arr)[I]) {}
    

    现在您将能够使用I 并且sizeof Arr 将报告就好像它在函数的局部变量堆栈上一样。

    您可能不想这样做的原因是每个大小的数组都会创建一个新的函数副本。可能会导致大量膨胀。

    但我使用这种技术来制作 constexpr 字符串类型。

    【讨论】:

    • 不要高估代码膨胀。它通常比人们想象的要小得多:)
    • “你可能不想这样做的原因是每个大小的数组都会创建一个新的函数副本。可能导致大量膨胀。”你有什么解决办法吗?我只想要一个干净、优化的代码。此模板运行良好,但“可能导致大量膨胀”。
    • @AliPadida - 不。除了使用迭代器或指针/大小参数之外,真的没有办法解决它。就速度而言,两者都有可能得到优化。
    【解决方案4】:

    您可以使用reference_wrapper 来模拟引用。这个例子取自http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper,展示了如何使用它来访问具有多个索引的容器。我假设这就是您希望排序函数执行的操作:对另一个容器中的项目的引用进行排序。

    #include <algorithm>
    #include <list>
    #include <vector>
    #include <iostream>
    #include <numeric>
    #include <random>
    #include <functional>
    
    int main()
    {
        std::list<int> l(10);
        std::iota(l.begin(), l.end(), -4);
    
        std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
        // can't use shuffle on a list (requires random access), but can use it on a vector
        std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
    }
    

    【讨论】:

    • 我觉得有点难。这里只是初学者。谢谢。
    猜你喜欢
    • 2013-05-10
    • 2016-02-06
    • 2014-03-26
    • 2018-11-13
    • 2018-05-25
    • 2014-02-25
    • 1970-01-01
    相关资源
    最近更新 更多