0

I have 2 containers with diffrent types, and i need to compare between the types.

Code example:

class A{


private:

std::list<Course*>  courses;
};

The std::list is just example its can be std::vector

And therefore i try to do this if :

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

And this is not the right way i guess.. How can i fix that ? Thanks.

user11001
  • 372
  • 3
  • 14

1 Answers1

0

I hope I understood what you are asking correctly.

Generally, if your code asks you to perform type casts or type comparisions, it indicates a design problem.

You can solve this by using generic code, that makes as few assumptions about the code as possible. To do this, use the generic container adapters in std (std::begin, std::end) and generic algorithms (see std::find, std::accumulate, std::for_each, std::transform).

You can also typedef your data type and write everything in terms of the typedef (and related types).

Example:

class A{
    typedef std::list<Course*> courseptr_sequence; // consider 
        // either shared_ptr or unique_ptr instead of raw*


    courseptr_sequence  courses;
public:
    // an advanced course is - for this example
    // a course where Course::advanced() evaluates to true
    void find_advanced_courses(std::ostream& out) const
    {
        auto advanced = std::find_if(std::begin(courses), std::end(courses),
            [](Course* p){ return p->advanced(); });
        if(advanced != std::end(courses))
            out << *(*advanced);
    }
};

With this kind of implementation, you can change the container type in the typedef and the rest will compile alright.

Edit: If your question was about two different container types where you compare elements at corresponding positions (and the elements have different types), the answer is to use std::equal with a custom comparison functor.

Edit (see reference1 and reference2):

template <class N>
struct is_list { static const int value = 0; };

template <class N, class A>
struct is_list<std::list<N, A> > { static const int value = 1; };

template <typename T> using negate = std::integral_constant<bool, !T::value>;

template<typename C>
void user3630497_sort(std::enable_if<
         negate<is_list<C>::value>::value, C>::type& container)
{
    std::sort(std::begin(container), std::end(container));
}

template<typename C>
void user3630497_sort(std::enable_if<
         is_list<C>::value, C>::type& container)
{
    container.sort();
}

usage:

std::list<Course*> l;
user3630497_sort(l);

std::vector<Course*> v;
user3630497_sort(l);
Community
  • 1
  • 1
utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • 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 – user11001 May 16 '14 at 15:48
  • It's the x-t problem :). I am editting my answer to solve your actual problem. – utnapistim May 16 '14 at 16:31