c++/ algorithm/ iterator/ set/ intersection

I'm currently attempting to find the intersection of two ordered sets of points in C++. I've attempted to use STL set_intersection, but I get the error: "No viable overloaded '='"

I then attempted to write my own version of set_intersection, called myset_intersection, so that I could hone in on the problem. I get the same error message, only when I click on it I'm shown another error message: "Candidate function not viable: 'this' argument has type 'const std::__1::__tree_const_iterator *, long>::value_type' (aka 'const Point'), but method is not marked const".

I've included my point class below, as well as the overloaded assignment operator and myset_intersection.

Any help is much appreciated.

struct Point{
    int x;
    int y;

    Point& operator=(const Point& p)  //Candidate function not viable...
    {
        x = p.x;
        y = p.y;
        return *this;
    }
};

using Iterator = set<Point,Point_order>::iterator;
Iterator myset_intersection(Iterator first1, Iterator last1, Iterator first2, Iterator last2, Iterator output)
{
    while(first1 != last1 && first2 != last2)
    {
        if(point_compare(*first1, *first2))
            ++first1;

        else{
            if(!(point_compare(*first2,*first1)))
            {
                *output = *first1;  //No viable overloaded '='
                ++output;
                ++first1;
            }
            ++first2;
        }
    }
    return output;
}

struct Point_order{
    bool operator()(const Point& a, const Point& b) const
    {
        if(a.x == b.x) return a.y < b.y;
        return a.x < b.x;
    }
};


You are trying to assign to a const Point .

Elements of a set can not be changed through iterators. Sets (and maps) are implemented through red-black trees, and the position of an element in the tree depends on the value of the key (sets have only keys). If you could modify the key, the tree would have to detect this and rearrange itself, or it would break.

Even though std::set<Point> has a separate iterator and a const_iterator , the data type of std::set<Point>::iterator::operator*() (the result of *output ) is const Point .

This has been true since 1998 ( https://cplusplus.github.io/LWG/issue103 )

If you want an out parameter of type Point , use a reference, not a set iterator.

std::set_intersection should work with this Point class

struct Point
{
  int x, y;
  Point(void) : x(0), y(0) {}
  Point(const int &x, const int &y) : x(x), y(y) {}
 ~Point(void) {}
  Point& operator=(const Point &P) { x=P.x; y=P.y; return *this; }
  bool operator==(const Point &P) const { return ((x == P.x) && (y == P.y)); }
  bool operator<(const Point &P) const { return ((x == P.x) ? (y < P.y) : (x < P.x)); }
};

But you can implement myset_intersection as:

using Const_Iterator = set<Point>::iterator;
template<typename Iterator> Iterator myset_intersection(Const_Iterator first1, Const_Iterator last1, Const_Iterator first2, Const_Iterator last2, Iterator output)
{
  while ((first1 != last1) && (first2 != last2))
  {
    if ((*first1) == (*first2))
    {
      (*output) = (*first1); ++output; ++first1; ++first2;
    }
    else
    {
      if ((*first1) < (*first2)) ++first1;
      else ++first2;
    }
  }
  return output;
}

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Error: No viable overloaded = Error: no viable overloaded operator[] no viable overloaded '=' error C++ No viable overloaded '=' error with map key set_intersection error when computing the intersection between two sets How to find intersection of sets Efficient intersection of two sets No Viable Overloaded +=? No viable overloaded "=" No viable overloaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM