Queue without duplicate entries

2

I am trying to implement a queue so that repeated objects can not be inserted. I have this implementation that I found but it gives me an error:

    std::queue<punto> q;
    std::set<std::reference_wrapper<punto>> s;
    // to add:

    void add(punto const & x)
    {
        if (s.find(x) == s.end())
        {
            q.push(x);
            s.insert(std::ref(q.back()));  // or "s.emplace(q.back());"
        }
    } 

The s.find(x) tells me no overloaded function instance.

    
asked by Don Protón 20.04.2018 в 14:10
source

1 answer

3

Indeed, std::set does not have a find method that you can use since it is a container that is already indexed (and unlike std::map has no key, then searching for an element does not make sense).

However, if you have a count method that works for your purposes:

if (s.count(x) == 0)
{
    q.push(x);
    s.insert(std::ref(q.back()));  // or "s.emplace(q.back());"
}

Since set does not support duplicates, count could only return 0 or 1.

    
answered by 20.04.2018 / 14:40
source