c++ - STL 容器作为模板参数

标签 c++ templates stl

我正在尝试为 STL 容器编写一个选择排序模板函数 (仅适用于 vector 、列表)这是我的代码

 template<typename T, template <T, typename =std::allocator<T>> class TContainer>


  void selection_sort(TContainer &vla,int length, bool (*comparisonFcn)(T,T)){
     int lowest_index;

      for(int i=0;i<length;i++){
         lowest_index=i;
         for(int j=i+1;j<length;j++){
            if(comparisonFcn(vla[j],vla[lowest_index])){
                lowest_index=j;
           }
         }
        std::swap(vla[i],vla[lowest_index]);
        std::cout<<i <<" "<<vla[i]<<std::endl;
}
 }


 //ascending function
 template <typename T>
bool ascending(T x,T y){
 return x<y;
 }
//descending function
template <typename T>
bool descending(T x,T y){
return x>y;
}

int main()
{

std::vector<int>vec={1,2,3,4};
selection_sort(vec,4,descending);
return 0;
  }

我遇到以下错误:

 /home/ubuntu/workspace/hello-cpp-world.cc:23:32: error: variable or field ‘selection_sort’ declared void
  void selection_sort(TContainer const &vla,int length, bool (*comparisonFcn)(T, T)){

 /home/ubuntu/workspace/hello-cpp-world.cc:23:43: error: expected primary-expression before ‘int’
  void selection_sort(TContainer const &vla,int length, bool (*comparisonFcn)(T, T)){
                                       ^
  /home/ubuntu/workspace/hello-cpp-world.cc:23:62: error: ‘comparisonFcn’ was not declared in this scope
    void selection_sort(TContainer const &vla,int length, bool (*comparisonFcn)(T, T)){
                                                          ^
   /home/ubuntu/workspace/hello-cpp-world.cc:23:78: error: expected primary-expression before ‘,’ token
   void selection_sort(TContainer const &vla,int length, bool (*comparisonFcn)(T, T)){
                                                                          ^
  /home/ubuntu/workspace/hello-cpp-world.cc:23:81: error: expected primary-expression before ‘)’ token
   void selection_sort(TContainer const &vla,int length, bool (*comparisonFcn)(T, T)){

  ^
  /home/ubuntu/workspace/hello-cpp-world.cc:23:81: error: expression cannot be used as a function
 /home/ubuntu/workspace/hello-cpp-world.cc: In function ‘int main()’:
  /home/ubuntu/workspace/hello-cpp-world.cc:54:37: error: ‘selection_sort’ was not declared in this scope

是否可以为 STL 容器将这些类型的函数模板化为参数。有人能告诉我如何在这些情况下编写正确的模板化格式吗?

最佳答案

您应该将 selection_sort 声明更改为

template<typename T, template <typename U, typename =std::allocator<U>> class TContainer>
void selection_sort(TContainer<T> &vla,int length, bool (*comparisonFcn)(T,T)){
    // as before
}

TContainer 是一个模板-模板参数,其参数还需要以 typenameclass 为前缀。这是fully working example您的代码。

注意:建议您为 selection_sort 提供与 std::sort 相同的签名,即使用迭代器和通用函数对象作为参数,例如something like

template<class FwdIt, class Compare = std::less<>>
void selection_sort(FwdIt first, FwdIt last, Compare cmp = Compare{})

关于c++ - STL 容器作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366902/

相关文章:

c++ - 需要帮助调试从 const char* 到 char* [-fpermissive] 的无效转换

c++ - 如何专门化依赖于类的静态数据成员的模板?

c++ - 在库 API 中传递 std::string

c++ - 为什么 std::ofstream 添加额外的#13(换行符)字符?

C++ 从磁盘读取文件并将其写入共享内存

c++ - 从特定范围内生成特定数量的唯一随机数

c++ - 推导嵌套模板的模板参数失败

c++ - 在 C++ 中使用 new 分配内存和重新解释指针

c++ - 递减结束迭代器

c++ - 我如何使用 std::rotate 来旋转第一个成员为 const 的对数组?