Question

struct Cord
    {
        int x_cord;
        int y_cord;
        Cord(int x = 0,int y = 0):x_cord(x),y_cord(y) {}
        bool operator < (const Cord& cord) const
        {
            if (x_cord == cord.x_cord)
            {
                return y_cord < cord.y_cord;
            }
            return x_cord < cord.x_cord;
        }
        bool operator == (const Cord& cord) const
        {
            return x_cord == cord.x_cord && y_cord == cord.y_cord;
        }
    };
std::set<Cord> cordRes,cordTmp;//initialized before
std::set<Cord> cordItersect;
std::set_intersection(cordRes.begin(),cordRes.end(),cordTmp.begin(),cordTmp.end(),cordRes.begin(),cordItersect.begin());

The compilation for above code fails.Any proposiotons how to use set_intersection correctly in the example? Thanks

Was it helpful?

Solution

Well one problem is that set_intersection only takes five arguments. The other problem is that you need some mechanism to create the elements you are inserting to cordItersect. In your case that would be to use std::inserter.

std::set_intersection(cordRes.begin(), cordRes.end(),
    cordTmp.begin(), cordTmp.end(),
    std::inserter(cordItersect, cordItersect.end()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top