查看二叉树的源码,发现如下函数:
//definition of BTR,in case you'd want to know
template< class Type>
struct BTR
{
// The item saved to that specifiec position into the tree
Type value;
// Points to the left leaf
BTR<Type>* left;
// Points to the right leaf
BTR<Type>* right;
};
//why typename?
template< class Type>
BTR<Type>* CreateEx(typename const std::vector<Type>::iterator start,typename const std::vector<Type>::iterator end)
{
//definition
}
现在,让我困惑的是这个函数的参数。 为什么需要关键字 typename? 因为如果我删除这两个类型名称,我的编译器就会开始提示并说我应该在标识符“开始”之前放置一个“)”。 如果我更改了参数,使函数使用两个 vector 而不是两个迭代器并删除了类型名,我的编译器就会停止提示(当然,该函数不再起作用)。
// perfectly acceptable!
template< class Type>
BTR<Type>* CreateEx( const std::vector<Type> start, const std::vector<Type> end)
看来我需要关键字,因为函数需要两个迭代器。 但是为什么在这种情况下需要这个关键字呢?
最佳答案
因为编译器不知道 std::vector
关于c++ - 将迭代器传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3457325/