我是新来的STL。我正在尝试编写一个以矢量迭代器为参数的例程。也就是说,我需要传递vector.begin()和vector.end()。想不通为什么我的代码不工作:C++向量迭代器函数模板
template<typename T, typename Iter> void VectorQuickSort(Iter itL, Iter itR){
const int nSize = (itR - itL);
if(nSize<2) return;
Iter iBeginning(itL), iEnd(itR);
--itR;
T tPivot = *(itL + (nSize/2));
while(itL <= itR){
while(*itL < tPivot) ++itL;
while(*itR > tPivot) --itR;
if(itL <= itR){
std::iter_swap(itL,itR);
++itL;
--itR;
}
}
VectorQuickSort(iBeginning,itR);
VectorQuickSort(itL,iEnd);
}
而在main()
我只需拨打VectorQuickSort(vInput.begin(),vInput.end());
。编译器告诉我error: no instance of function template "VectorQuickSort" matches the argument list
。任何帮助赞赏:)
编辑:作为一个潜在的尝试使用上面的代码的人的警告:即使你应用提供的答案,还有排序算法本身有问题。我无法正确地将工作C版本转换为STL样式。
+1(并且我希望我可以再次+1这为我节省时间写出非常逐字逐句的我即将开始)。说实话,他甚至不需要“知道”这种类型,如果这足够简洁。示例可以[在这里找到](http://ideone.com/BNMEjg) – WhozCraig 2013-03-23 21:29:07
@sftrabbit非常感谢! – 2013-03-23 23:28:11