Beach Williams Beach Williams - 27 days ago 8
C++ Question

No match for operator "!=" (c++ iterators)

The program should be a binary search in a vector. In the end it prints the found element. My code is:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
vector<string> s{"a","b","c"};

auto beg=s.begin(), end=s.end(), mid=s.begin()+(end-beg)/2;
auto sought='c';

while(*mid!=sought && mid!=end)



return 0;

It says that the error is that operator != has no match at (*mid!=sought && mid!=end). If I try to do it on a simple string instead of a vector it works.

Answer Source

The type of 'c' is char. The type of *mid is std::string. operator!= is not defined between char and std::string.

You can change sought to:

auto sought = "c"; // C-style string

Or to:

using namespace std::literals;
auto sought = "c"s; // `std::string`