mercredi 31 décembre 2014

Is it possible for C++ to write a template that takes a Container as parameter?

I would try to put my point clear by an example:


We have



template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);


But I'm think of if it is ok to make it more convenient



template <typename T> void sort(std::vector<T>& container) {
std::sort( container.begin(), container.end() );
}
template <typename T> void sort(std::list<T>& container);
template <typename T> void sort(std::array<T>& container);
//e.t.c


You know there are many container types, it is possible to code once for all the container types?



void sort(ContainerType<ElementType> &container);
//and container should have begin() and end() methods, otherwise the compiler would warn me.

Aucun commentaire:

Enregistrer un commentaire