Template specialization for std :: less in C ++ 11 using a template

I have a Matrix class that comes from the Eigen template:

template<typename T,
         int _Rows = Eigen::Dynamic,
         int _Cols = Eigen::Dynamic>
class Matrix : public Eigen::Matrix<T, _Rows, _Cols>

I need to use this type as the key for the container std::map, so I need a comparator object. For this purpose, I would like to specialize std::less. A project that does not compile looks like this so that you understand:

template<typename Matrix<typename T,
                         int _Rows = Eigen::Dynamic,
                         int _Cols = Eigen::Dynamic> > >
struct less
{
    bool operator()(const Matrix<T,
                                 Rows,
                                 Cols>& lhs,
                    const Matrix<T,
                                 Rows,
                                 Cols>& rhs) const;
    {
      Matrix<T,
             Rows,
             Cols>::const_iterator lhsIt = lhs.begin();
      Matrix<T,
             Rows,
             Cols>::const_iterator rhsIt = rhs.begin();
      for (;
           lhsIt != lhs.end();
           ++lhsIt, ++rhsIt)
      {
        if (*lhsIt < *rhsIt)
        {
          return true;
        }
      }
      return false;
    }
};

The problem is that I want to specialize std::lesswith a template. What is the correct way to code this? Do I need to resort to specialized specialization?

I will also need to specialize in a std::hashsimilar way to be able to use std::map.

+4
2

, std::less .

. std::less " <" ", <, , , < .

operator<, std::map.

std::hash , std::map.

, . unordered_map.

, . [2, 1] < [1, 2] [1, 2] < [2, 1]. , , .

+7

template <typename T, int Row, int Col>
struct less<Matrix<T, Row, Col>>
{
    bool operator()(const Matrix<T, Row, Col>& lhs,
                    const Matrix<T, Row, Col>& rhs) const
    {
        // implementation:
        return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
    }
};

.

, : ( ).

+5

All Articles