PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

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

 November 07, 2022     c++, find, iterator, set, struct     No comments   

Issue

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;
    }
}


Answered By - Parisa.H.R
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing