Search code examples
c++structiteratorsetfind

How can i properly use the .find() function from set in C++?


Here is the struct for 'point'

struct point
{
  double x;
  double y;
};

and here is the function that produces an error, there is an issue with my use of .find() as show in the pic below. When I hover over the error it says "In template: invalid operands to binary expression ('const point' and 'const point')"

bool isChecked(point left, point right, set<vector<point>>const& inSet)
{
  // Check both possible arrangements of points
  vector<point> tmp1 = {left, right};
  vector<point> tmp2 = {right, left};

  // .find() returns an iterator up to the element found
  // If not found, return element after last, .end()
  auto first = inSet.find(tmp1);
  auto second = inSet.find(tmp2);

  // Check if elements were checked already
  if (first != inSet.end() || second != inSet.end())
      return true;

  return false;
}

Here is the error provided by compiler:

C:/msys64/mingw64/include/c++/12.2.0/bits/predefined_ops.h:45:23: error: no match for 'operator<' (operand types are 'const point' and 'const point')
   45 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
In file included from C:/msys64/mingw64/include/c++/12.2.0/string:47:
C:/msys64/mingw64/include/c++/12.2.0/bits/stl_iterator.h:1246:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __normal_iterator<_IteratorL, _Container>&, const __normal_iterator<_IteratorR, _Container>&)'
 1246 |     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
C:/msys64/mingw64/include/c++/12.2.0/bits/stl_iterator.h:1246:5: note:   template argument deduction/substitution failed:
C:/msys64/mingw64/include/c++/12.2.0/bits/predefined_ops.h:45:23: note:   'const point' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
   45 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
C:/msys64/mingw64/include/c++/12.2.0/bits/stl_iterator.h:1254:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __normal_iterator<_Iterator, _Container>&, const __normal_iterator<_Iterator, _Container>&)'
 1254 |     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
C:/msys64/mingw64/include/c++/12.2.0/bits/stl_iterator.h:1254:5: note:   template argument deduction/substitution failed:
C:/msys64/mingw64/include/c++/12.2.0/bits/predefined_ops.h:45:23: note:   'const point' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
   45 |       { return *__it1 < *__it2; }
      |                ~~~~~~~^~~~~~~~
ninja: build stopped: subcommand failed.

Solution

  • you need to add this to your code:

    bool operator<(const point& lhs, const point& rhs)
    {
      return lhs.x < rhs.x;
    }
    

    to overload the < operator.

    and then you write your function and you can write it like this:

    bool isChecked(point left, point right, set<vector<point>> inSet)
    {
      // Check both possible arrangements of points
      vector<point> tmp1 = {left, right};
      vector<point> tmp2 = {right, left};
    
        if (inSet.find(tmp1) != inSet.end() || inSet.find(tmp2) != inSet.end())
        {
            return true;
        }
        else
        {
            return false;
        }
    }