Search code examples
c++mingw32

C++ vector - find ("no match for 'operator =='")


So I'm pretty new to C++, so I wanted to loop through a multidimensional vector that I have, but I'm getting errors such as

stl_algo.h

error: no match for 'operator==' (operand types are std::vector<std::basic_string<char> >' and 'const std::basic_string<char>'

There is lots of errors, here is the code:

mClass.h

std::vector<std::vector<std::string> > aData;

mClass.cpp

bool mClass::checkVector(std::string k)
{
    if (std::find(mClass::aData.begin(), mClass::aData.end(), k) != mClass::aData.end())
    {
        return true;
    }
    return false;
}

Solution

  • mClass::aData.begin() and mClass.aData.end() return iterators over vectors, not iterators over strings. There is no operator == to compare a vector and a string. Hence the error.

    You'll need to iterate through the vectors. Assuming you have C++11 support:

    bool mClass::checkVector(std::string const& k)
    {
      for (auto const& vec : aData) { // Each `vec` is a std::vector<std::string>
        for (auto const& str : vec) { // Each `str` is a std::string
          // compare string represented by it2
          if (str == k) {
            return true;
          }
        }
      }
      return false;
    }