rasevanth
rasevanth

Reputation: 31

stl containers as template parameter

i am trying to write a selection sort template function for stl containers (only for vectors , lists) this is my code

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

i have got the following errors:

 /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

is it possible to template these type of functions for stl containers as parameters.could anyone tell me how to write the correct format for templating in these situations?

Upvotes: 2

Views: 537

Answers (1)

TemplateRex
TemplateRex

Reputation: 70516

You should change your declaration of selection_sort into

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
}

The TContainer is a template-template parameter, for which the parameters also need to be prefixed with either typename or class. Here's the fully working example of your code.

Note: it is recommended that you give selection_sort the same signature as std::sort, i.e. using iterators and a general function object as parameters, e.g. something like

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

Upvotes: 3

Related Questions