C++发送数组,按值运行,不通过引用 我有一个C++问题。< /P>

C++发送数组,按值运行,不通过引用 我有一个C++问题。< /P>,c++,C++,我有一个对数组排序的函数,但我不想处理原始数组。我想通过值而不是引用将数组发送到函数。请帮帮我 int bogoSort(int tab[], int n){ int iloscOperacjiDominujacych = 0; cout<<"rozpoczalem algorytm BogoSort"<<endl; srand (time(NULL)); named (outer) while(true){ // cout

我有一个对数组排序的函数,但我不想处理原始数组。我想通过值而不是引用将数组发送到函数。请帮帮我

int bogoSort(int tab[], int n){
int iloscOperacjiDominujacych = 0;
    cout<<"rozpoczalem algorytm BogoSort"<<endl;

    srand (time(NULL));
    named (outer)
    while(true){
 //       cout<<"Zaczal sie while"<<endl;
        named (inner)
        for(int i = 0; i < n; i++){
            if(i == n-1){
                break (outer);
            }
            if (tab[i] > tab[i+1]){
                break (inner);
            }
        }
        for(int i = n-1; i > 0; i--){
            iloscOperacjiDominujacych++;
            //operacja dominujaca to zamiana dwoch elementow w tablicy, wykonuje sie ZAWSZE najwiecej razy i jest najbardziej zlozona
            int swapPostition =  rand() % (i+1); //wylosowanie liczby miedzy <0;i> nalezacej do calkowitych
            int temp = tab[i];
            tab[i] = tab[swapPostition];
            tab[swapPostition] = temp;
        }
    }
//    cout<<"Wykonal sie while"<<endl;
    show(tab,n);
    return iloscOperacjiDominujacych;
}

< >在C++中没有按值传递数组的方法。如果你不想修改原始数组,那么你必须自己制作一个单独的拷贝,并使用该拷贝,或者如果你有C++ 11,并且使用它来复制和返回它的STD::vector或STD:::Advices数组,因为你可以复制STD::vector或数组。

在C++中,没有按值传递数组的方法。如果您不想修改原始数组,那么您要么自己制作一个单独的副本并处理副本,要么使用std::vector或std::array(如果您有C++11)并按值传递和返回它,因为您可以复制std::vector或array。

您不能按值传递C样式数组。故事结束了

但是,您可以通过包含数组的值传递类类型的变量。利用此漏洞的最简单方法是使用std::array:

该类基本上类似于struct{int data[10];};,因此,如果您真的愿意,您甚至可以自己滚动它。

您不能按值传递C样式数组。故事结束了

但是,您可以通过包含数组的值传递类类型的变量。利用此漏洞的最简单方法是使用std::array:


该类基本上类似于struct{int data[10];};,因此,如果您真的愿意,您甚至可以自己使用它。

C++谈到函数声明

确定每个参数的类型后,“T数组”或“函数返回T”类型的任何参数都被调整为“指向T的指针”或“指向函数返回T的指针”[dcl.fct]8.3.5/5

因此,当你想通过一个数组C++时,它会传递一个指针指向原始数组的第一个元素,而不是复制一个拷贝并按值传递,这将与其他类型一致。这是C兼容的一个不幸结果,我不知道为什么C认为这种不一致性是个好主意

无论如何,C++提供了STD::数组,用于静态大小数组和STD::动态大小数组的向量。由于C数组的奇怪之处,您应该尽可能避免使用它们。很少有一种情况下你无法避免它们

int tab[]是一个绑定未知的数组,因此不能使用静态大小的std::数组,必须使用std::vector:

正如你所看到的,我在内部使用一个向量,我有一个bogoSort重载,它接受一个向量。将此与将副本设置为原始阵列进行比较:

int bogoSort(int const *tab,int n) {
  int *tab_copy = new int[n];
  std::copy(tab,tab+n,tab_copy);             // manual copying
  bogoSort_impl(tab_copy,n);                 // not overloading, hidden internal function
  delete [] tab_copy;                        // resource cleanup. We're not exception safe!
}

// or

int bogoSort(int const *tab,int n) {
  // unqiue_ptr for exception safety
  std::unqiue_ptr<int[]> tab_copy = std::unqiue_ptr<int[]>(new int[n]);
  std::copy(tab,tab+n,tab_copy.get());
  bogoSort_impl(tab_copy.get(),n);
}

同样,您真的不应该使用C数组。它们太麻烦了,没有任何好处。

C++谈到函数声明

确定每个参数的类型后,“T数组”或“函数返回T”类型的任何参数都被调整为“指向T的指针”或“指向函数返回T的指针”[dcl.fct]8.3.5/5

因此,当你想通过一个数组C++时,它会传递一个指针指向原始数组的第一个元素,而不是复制一个拷贝并按值传递,这将与其他类型一致。这是C兼容的一个不幸结果,我不知道为什么C认为这种不一致性是个好主意

无论如何,C++提供了STD::数组,用于静态大小数组和STD::动态大小数组的向量。由于C数组的奇怪之处,您应该尽可能避免使用它们。很少有一种情况下你无法避免它们

int tab[]是一个绑定未知的数组,因此不能使用静态大小的std::数组,必须使用std::vector:

正如你所看到的,我在内部使用一个向量,我有一个bogoSort重载,它接受一个向量。将此与将副本设置为原始阵列进行比较:

int bogoSort(int const *tab,int n) {
  int *tab_copy = new int[n];
  std::copy(tab,tab+n,tab_copy);             // manual copying
  bogoSort_impl(tab_copy,n);                 // not overloading, hidden internal function
  delete [] tab_copy;                        // resource cleanup. We're not exception safe!
}

// or

int bogoSort(int const *tab,int n) {
  // unqiue_ptr for exception safety
  std::unqiue_ptr<int[]> tab_copy = std::unqiue_ptr<int[]>(new int[n]);
  std::copy(tab,tab+n,tab_copy.get());
  bogoSort_impl(tab_copy.get(),n);
}

同样,您真的不应该使用C数组。它们太麻烦了,没有任何好处。

如何有效地复制阵列简单阵列而不是任何集合?感谢您的快速回答。@RobertKilar创建另一个大小相同的数组,使用std::copy将元素从原始数组复制到新数组,并将副本传递到函数中。如何有效地复制数组简单数组而不是任何集合?谢谢您的快速回答。@RobertKilar创建另一个相同大小的数组,使用std::copy将元素从原始数组复制到新数组,并将副本传递到函数中。
int bogoSort(int const *tab,int n) {
    std::vector<int> tab_copy(tab,tab+n);
    bogoSort(tab_copy);
}

int bogoSort(std::vector<int> tab) {
    ...
}
int bogoSort(int const *tab,int n) {
  int *tab_copy = new int[n];
  std::copy(tab,tab+n,tab_copy);             // manual copying
  bogoSort_impl(tab_copy,n);                 // not overloading, hidden internal function
  delete [] tab_copy;                        // resource cleanup. We're not exception safe!
}

// or

int bogoSort(int const *tab,int n) {
  // unqiue_ptr for exception safety
  std::unqiue_ptr<int[]> tab_copy = std::unqiue_ptr<int[]>(new int[n]);
  std::copy(tab,tab+n,tab_copy.get());
  bogoSort_impl(tab_copy.get(),n);
}