0

vec is std::vector<std::string> type. I'm trying to remove strings which not equal to query.

    Compare compare(0, i, query);
    vec.erase(std::remove(vec.begin(), vec.end(), compare));

This is my Functor:

class Compare {
private:
    size_t m_from;
    size_t m_to;
    std::string m_query;

public:
    Compare(size_t from, size_t to, std::string& query) : m_from(from), m_to(to), m_query(query)
    {}

    std::string operator()(std::string s)
    {
        
        if(s.substr(m_from, m_to) != m_query.substr(m_from, m_to))
        {
            return s;
        }
    } };

I get this compile error:

error: no match for ‘operator==’ (operand types are ‘std::__cxx11::basic_string<char>’ and ‘const Compare’)
  241 |  { return *__it == _M_value; }

I understand that I'm missing operator==, but why and where?

Thanks!

kobi
  • 101
  • 7
  • 1
    [`std::remove`](https://en.cppreference.com/w/cpp/algorithm/remove) doesn't have a signature that accepts a comparator. Did you mean `std::remove_if` (ibid.)? – Fred Larson Feb 09 '22 at 13:43
  • 1
    `std::remove(vec.begin(), vec.end(), compare)` is attempting to remove the object `compare` from your vector. That's why the compiler is looking for a way to see if any of your strings are equal to a `Compare`. – Drew Dormann Feb 09 '22 at 13:44
  • 1
    You are also using the wrong form of `std::vector::erase`. Passing **one** parameter (an iterator) will only erase **one** element. See the duplicate for the correct syntax. – Drew Dormann Feb 09 '22 at 13:48

0 Answers0