问题描述
我有一个C ++的问题。
I have a problem with C++.
我有一个函数排序数组,但我不想在原始数组上工作。我想发送数组到函数的值不通过引用。请帮帮我。
I have a function which sorts the array, but I don't want to work on an original array. I want to send the array to the function by value not by reference. Please help me.
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 ++中的值的数组。如果你不想修改原始数组,那么你必须自己创建一个单独的副本并处理副本,或者使用 std :: vector
或 std :: array
(如果你有C ++ 11)并传递并返回值(因为你可以复制 std: :vector
或 array
)。
There is no way to pass an array by value in C++. If you don't want to modify the original array, then you either have to make a separate copy yourself and manipulate the copy, or use std::vector
or std::array
(if you have C++11) and pass and return it by value (because you can copy std::vector
or array
).
这篇关于C ++通过值发送数组到函数,而不是通过引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!