user11001
user11001

Reputation: 372

sort container of pointers c++ and compare type

i need sort container. but i get the type of container from template.. beacouse this i need to use std::sort for std::vector and name.sort for std::list. and therefore i need check the type of container in order to chose what to do.

i try to do:

class sor {
public:
    sor(){}
    bool operator( )(Course* l, Course* r) {
        return l->getNumber() < r->getNumber();
    }
};



if (typeid(courses) == std::list){
    //do something...
}
else{
   //do...
} 

how can i do that? Thanks

Upvotes: 2

Views: 326

Answers (3)

Shoe
Shoe

Reputation: 76250

The std::sort algorithm is somehow container independent (it requires iterator to meet the requirements of ValueSwappable and RandomAccessIterator) so you can use it like:

std::sort(container.begin(), container.end(), sor());

Unfortunately std::list does not satisfy RandomAccessIterator, though.


If you have a template function that accepts any container type, you can provide specializations to it. Alternatively, with type traits you can provide more general template "overloads":

template<class... Args, template<class...> class Container>
void sort(Container<Args...>&) { ... }

template<class... Args>
void sort(std::list<Args...>&) { ... }

Live demo

Upvotes: 1

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42909

You could use type traits and tag dispatching see code below:

#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <functional>

struct vectorlike_tag { };
struct listlike_tag   { };

template <typename C> struct container_traits;

template <typename T, typename A>
struct container_traits<std::vector<T, A>> {
  typedef vectorlike_tag category;
};

template <typename T, typename A>
struct container_traits<std::list<T, A>> {
  typedef listlike_tag category;
};

template <typename Container, typename Compare>
void sort_helper(Container& c, Compare f, vectorlike_tag) {
  std::sort(c.begin(), c.end(), f);
}

template <typename Container, typename Compare>
void sort_helper(Container& c, Compare f, listlike_tag) {
  c.sort(f);
}

template <typename Container, typename Compare>
void sort_container(Container &c, Compare f) {
  sort_helper(c, f, typename container_traits<Container>::category());
}

template<class Container>
void sort_container(Container &c)
{
  sort_helper(c, std::less<typename Container::value_type>(), typename container_traits<Container>::category());
}

int main()
{
  std::vector<int> v{ 4, 3, 7, 8, 9 };
  sort_container(v);
  for (auto e : v) std::cout << e << " ";
  std::cout << std::endl;
  std::list<int> lst{ 4, 3, 7, 8, 9 };
  sort_container(lst, std::greater<int>());
  for (auto e : lst) std::cout << e << " ";
  std::cout << std::endl;
  return 0;
}

Output:

3 4 7 8 9

9 8 7 4 3

Upvotes: 0

sam
sam

Reputation: 170

you could overload your own sort function

template<typename T, typename Compare>
void sort(std::list<T>& list, Compare compare)
{
    list.sort(compare);
}

template<typename T, typename Compare>
void sort(std::vector<T>& vector, Compare compare)
{
    std::sort(vector.begin(),vector.end(),compare);
}

Upvotes: 3

Related Questions