初学者,但在网上找不到答案。。有哪位好心人能帮我看看,不甚感激
template<int SIZE>
int(&a)[SIZE] insertSort(int(&arr)[SIZE])
{
return arr;
};
void main()
{
int arr[] = {2,4,56,65,3,6,9,4,3,5,8,4,2,1,0};
insertSort(arr);//提示找不到insertSort函数,如果将函数的返回值修改一下,编译就能通过
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你的函数写法也有问题,stackoverflow找来一个答案,亲测可运行
顺便叨逼一句,尽量不要这么做
void sort(const int *arr, int *sortedArr)
vector
第一行是这样的:template
<int SIZE>
既然是使用的
C++
,这里是可以不用指针做到类型安全的。如 @wangdai 说的vector
。至于返回,赋值什么的这些都不用管,最简单的直接使用out参数
当然,如果你必须要通过返回来到达高比格的效果,那就这样
老实说,正统的
C++
方式应该是