我是新來的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