mardi 28 juillet 2020

Adding another custom comparator for std::set::upper_bound or lower_bound for a set of structs

Here I've sorted a set of structs using a comparator functor. But the upper_bound and lower_bound methods of std::set also use this same comparator for finding out the element.

struct segment{
    int ax, ay;
    int bx, by;

    segment(){};
    segment(int a1, int a2, int b1, int b2)
    :ax(a1), ay(a2), bx(b1), by(b2){}
    
};

struct classCompare{
    bool operator()(const segment& l, const segment& r)
    {
            // I donot wish to use this comparison for upper_bound or lower_bound 
            // just want to include the else part
            if(l.ay == r.ay)
                return l.ax < r.ax;
            else
                return l.ay < r.ay;
    }
};
...
//(inside main)
   if(temp.ax > temp.bx)
            {
                int k = temp.ax;
                temp.ax = temp.bx;
                temp.bx = k;
            }
            segs_H.insert(temp);
        }            
    }

    int count(0);
    for(unsigned int i=0;i<segs_V.size();i++)
    {
        std::cout << segs_V[i].ax << "," << segs_V[i].ay << "  " << segs_V[i].bx << "," << segs_V[i].by << std::endl ;

        //Here
        auto iter_L = segs_H.lower_bound(segment(segs_V[i].ax, segs_V[i].ay, segs_V[i].ax,                                                        
                                                 segs_V[i].ay));
        auto iter_U = segs_H.upper_bound(segment(segs_V[i].bx, segs_V[i].by, segs_V[i].bx, 
                                                 segs_V[i].by));
        
        for(;iter_L != iter_U; iter_L++)
        {   
            std::cout << iter_L->ax << "," << iter_L->ay << "  " << iter_L->bx << "," << iter_L->by << std::endl ;
            if( iter_L->ax <= segs_V[i].ax && segs_V[i].ax <= iter_L->bx )
            {
                count++;
            }
        }
        std::cout << std::endl;
    }

Is there a way to define another comparator that can be used by upper_bound or lower_bound. I tried using std::upper_bound but that dosen't seem to properly iterate through the set.

Aucun commentaire:

Enregistrer un commentaire