U
U
using_namespace_boring2021-09-01 17:41:05
C++ / C#
using_namespace_boring, 2021-09-01 17:41:05

std find algorithm not working for string?

string str{"fdsfs dfgdf dgf"};
auto find=std::find(begin(str),end(str),"  ");

This code throws an error - pointer and int cannot be compared.
What's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-09-01
@using_namespace_boring

You are calling std::find , which is designed to find an element in the collection. And you try to look for a line there. This all almost compiles, because a string is a collection of characters, one could search for a single character on it. But you are passing a pointer (your string constant) there. The compiler cannot convert it to a symbol and swears at it.
For what you need, there is std::string::find . Those. you need to call str.find(" ").
Or look for a single character. Just do not forget to include algorithm, otherwise incomprehensible errors with iterators will come up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question