1

I'm trying to run this comparator:

if (std::find(movements->begin(), movements->end(), commandBuffer) != movements->end())

with:

const std::string movements[8] = {"north", "south", "east", "west", "n", "s", "e", "w"};

where commandBuffer is a std::string.

On MinGW32 9.2.0, I get the following:

error: no match for 'operator==' (operand types are 'const char' and 'const std::__cxx11::basic_string')

0

2 Answers 2

3

Either use std::cbegin and std::cend, or change your array into a std::array:

std::array<const std::string, 8> movements = {"north", "south", "east", "west", "n", "s", "e", "w"};
const std::string commandBuffer = "north";

if (std::find(movements.begin(), movements.end(), commandBuffer) != movements.end())
    std::cout << "Found!" << std::endl;
Sign up to request clarification or add additional context in comments.

Comments

3

movements is a pointer to the array of strings, basically:

const std::string* movements;

movements->begin() is then equal to (movements[0]).begin() and points to the first character of the first string, not the string itself. So you are iterating over the characters of the first string.

To iterate over the array of strings instead use begin(movemens), end(movements).

If you were used std::vector<string> movements or std::array<string, N>, your code would work also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.