There is also a problem with the way you write the function. I found an answer on stackoverflow. I can test it myself and run it
template<size_t n>
int (&insertSort(int (&arr)[n]))[n]
{
return arr;
}
http://stackoverflow.com/a/2302395
By the way, try not to do this
This kind of code is difficult to read, and this template is not very scientific. A new n will generate a new function
If you want to return an array, you can use a pointer, but try not to do this, because allocating memory within a function can easily cause memory leaks (unless it only allocates memory)
The correct way to return an array is to pass a pointer to allocated memory, like thisvoid sort(const int *arr, int *sortedArr)
Since C++ is used, type safety can be achieved here without using pointers. As @wangdai said vector. As for returns, assignments, etc., you don’t have to worry about them. The simplest way is to use out参数
There is also a problem with the way you write the function. I found an answer on stackoverflow. I can test it myself and run it
By the way, try not to do this
void sort(const int *arr, int *sortedArr)
vector
The first line is like this: template
<int SIZE>
Since
directly.C++
is used, type safety can be achieved here without using pointers. As @wangdai saidvector
. As for returns, assignments, etc., you don’t have to worry about them. The simplest way is to useout参数
Of course, if you have to go back to achieve the high-big effect, then do this
Honestly, the orthodox
C++
way should be