在 main 中读取函数模板

Read a function template in main?

本文关键字:函数模板 读取 main      更新时间:2023-10-16

好吧,我不是一个很有经验的程序员,C++(仍在学习)我知道所有的C库。

我想出了一个问题。以下模板是什么意思,我应该如何从 main 调用拆分函数?

template <typename T, size_t len> 
pair<size_t,size_t> 
split(T const (& arr)[len]) { 
//blah blah .. code ... blah blah 
} 

我知道我需要一个数组,但我如何在模板中传递它?我需要什么参数?

这是我在 main 中创建的数组

int N = 10;
int *array = new int[N];
//now I use I/O to pass the numbers in the array, but say we have them
 //Now I should call the template. I need to pass the array
   //to make the process?
 delete [] array;

你只需要堆栈上的数组,而不是堆上的数组。数组的大小应该在编译时知道。

const size_t N = 10;
int array[N];
// fill array
split(array);