简体   繁体   English

c ++向量函数模板的向量迭代器

[英]c++ vector iterator to function template

I'm new to STL. 我是STL的新手。 I'm trying to write a routine taking vector iterators as parameters. 我正在尝试编写一个以矢量迭代器为参数的例程。 Namely, I need to pass vector.begin() and vector.end(). 也就是说,我需要传递vector.begin()和vector.end()。 Can't figure why my code is not working: 无法理解我的代码无效的原因:

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);
}

And in main() I simply call VectorQuickSort(vInput.begin(),vInput.end()); main()我只需调用VectorQuickSort(vInput.begin(),vInput.end()); . Compiler tells me error: no instance of function template "VectorQuickSort" matches the argument list . 编译器告诉我error: no instance of function template "VectorQuickSort" matches the argument list Any help appreciated :) 任何帮助赞赏:)

EDIT: As a warning for someone potentially trying to use the code above: even if you apply the answer provided, there is still something wrong with the sorting algo itself. 编辑:作为对可能尝试使用上述代码的人的警告:即使您应用了提供的答案,排序算法本身仍然存在问题。 I fail to translate properly working C version into STL style. 我无法正确地将C版本转换为STL样式。

It can't deduce the template parameter T from those arguments. 它不能从这些参数中推导出模板参数T In fact, you don't even need T . 事实上,你甚至不需要T It is redundant because you can work out the type of T from Iter - after all, the iterator iterates over elements of type T . 它是多余的,因为你可以从IterT的类型 - 毕竟,迭代器迭代了T类型的元素。 Change it to just this: 将其更改为:

template<typename Iter>

Then, if you're using C++11, you can change the line that uses T to automatically deduce the type of tPivot : 然后,如果您使用的是C ++ 11,则可以更改使用T的行来自动推断tPivot的类型:

auto tPivot = *( itL + (nSize/2) );

If you don't have C++11 support, you can instead use std::iterator_traits to determine the appropriate type: 如果您没有C ++ 11支持,则可以使用std::iterator_traits来确定适当的类型:

typename std::iterator_traits<Iter>::value_type tPivot = *( itL + (nSize/2) );

You can perhaps simplify this with a typedef : 您可以使用typedef简化此操作:

typedef typename std::iterator_traits<Iter>::value_type value_type;
value_type tPivot = *( itL + (nSize/2) );

You need to write VectorQuickSort<int,/*iterator type*/>(vInput.begin(),vInput.end()); 您需要编写VectorQuickSort<int,/*iterator type*/>(vInput.begin(),vInput.end()); or whatever you'd like it to be templated above, notice you mention it's templated over T but the function has no way to deduce where/how T is templated... Either delete T and use auto or tell the function how is T templated 或者你想要它在上面模仿的任何东西,请注意你提到它的模板超过T但是该函数无法推断T的模板在哪里/如何...删除T并使用auto或告诉函数如何模板化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM