Visual C++ and Sorting std::list

Today I just feel like bloging (I think this is my 3rd today) so I feel lucky just see I had 2 comments on my blog this is best for today, so I’ll keep writing cause I think this is working (Donot try to disappoint me just if these helps post me comment :D)!!!
 
so If you look at my last blog in this Catagory youill see some Utils/ Wrapper on "std::list , std::hash_map , std::pair , std::string" some of them of slower than the MFC counterparts and some of them are lot faster (String is 1500% faster WOW! but List is slower but I like it!) but by being faster or slower I mean about 1000000 time operation which you never make :D.
 
Anyway today I’m gonna show you 2 new Utils:
 
1) First the way to sort the List of Referenece types like: List<int *>
 
This is the way:
 
    template<class Type>
    struct ReferenceTypeLess
      : public std::less<Type>
    {
      virtual bool operator()(const Type _Left, const Type _Right) const
      {
        return *_Left < *_Right;
      }
    };
 
just use it in this way:
 
  List<int *> listOfInts;
  … // (fill it here)
  listOfInts.sort(ReferenceTypeLess<int *>());
 
It will also work for References to class/ struct like: List<MyClass *> , just remember to override the "operator <" in the "MyClass".
 
 
2) The second is nothing special (DO you think all the rest was special?? :D) but helped me, hope help you to:
 
    //—————————————————————————
    template <class ValueType1, class ValueType2 = ValueType1, class ValueType3 = ValueType1, class ValueType4 = ValueType1>
    struct Rectangle
    {
      ValueType1 top;
      ValueType2 begin;
      ValueType3 bottom;
      ValueType4 end;
      Rectangle() { }
      Rectangle(ValueType1 top, ValueType2 begin, ValueType3 bottom, ValueType4 end) : top(top), begin(begin), bottom(bottom), end(end) { }
    };
    //—————————————————————————
    template <class ValueType>
    struct SymmetricalRectangle
      : public Rectangle<ValueType, ValueType, ValueType, ValueType>
    {
      SymmetricalRectangle() { }
      SymmetricalRectangle(ValueType top, ValueType begin, ValueType bottom, ValueType end) : Rectangle(top, begin, bottom, end) { }
      SymmetricalRectangle(ValueType value)
      {
        SetAll(value);
      }
      void SetAll(ValueType value)
      {
        top = value;
        begin = value;
        bottom = value;
        end = value;
      }
    };
    //—————————————————————————
    struct FloatRectangle
      : public SymmetricalRectangle<float>
    {
    public:
      FloatRectangle() : SymmetricalRectangle(0.0f) { }
      float Width() { return end – begin; }
      float Height() { return bottom – top; }
    };
    //—————————————————————————
    struct IntRectangle
      : public SymmetricalRectangle<int>
    {
    public:
      IntRectangle() : SymmetricalRectangle(0) { }
      int Width() { return end – begin; }
      int Height() { return bottom – top; }
    };
    //—————————————————————————
Those were Rectangle of everything.
 
 
Hope these will help,
 
Sadjad Bahmanpour
 

Leave a Reply