Why does find_if not work in my program?

I have a simple program that calls std::find_if, I think I pass the first two arguments as an iterator, and the third - how to predict, but the code still will not compile any ideas?

#include <string>
#include <cctype>
#include <algorithm>

bool notspace(char ch);
bool space(char ch);

int main()  {
    typedef std::string::const_iterator iter;
    iter i;
    std::string s = "ab c";
    i = std::find_if(i, s.end(),space);
    return 0;
}

bool space(char ch)  {
    return std::isspace(ch);
}

Error message:

q-isspace.cpp: In function β€˜int main()’:
q-isspace.cpp:12:38: error: no matching function for call to β€˜find_if(iter&, std::__cxx11::basic_string<char>::iterator, bool (&)(char))’
     i = std::find_if(i, s.end(),space);
                                      ^
In file included from /usr/include/c++/5/algorithm:62:0,
                 from q-isspace.cpp:3:
/usr/include/c++/5/bits/stl_algo.h:3806:5: note: candidate: template<class _IIter, class _Predicate> _IIter std::find_if(_IIter, _IIter, _Predicate)
     find_if(_InputIterator __first, _InputIterator __last,
     ^
/usr/include/c++/5/bits/stl_algo.h:3806:5: note:   template argument deduction/substitution failed:
q-isspace.cpp:12:38: note:   deduced conflicting types for parameter β€˜_IIter’ (β€˜__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >’ and β€˜__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >’)
     i = std::find_if(i, s.end(),space);
+4
source share
5 answers

You pass ithat is of type std::string::const_iterator(and also uninitialized) as the first parameter to std::find_if. Then you pass s.end()that returns std::string::iterator. Two iterators have different types, but std::find_ifexpects them to be of the same type.

The correct rule of thumb is to connect calls to begin()andend()

#include <string>
#include <cctype>
#include <algorithm>

bool notspace(char ch);
bool space(char ch);

int main()  {
    typedef std::string::const_iterator iter;
    iter i,j;
    std::string s = "ab c";
    i = std::find_if(s.begin(), s.end(),notspace);
    j = std::find_if(s.begin(), s.end(),space);
    return 0;
}

bool space(char ch)  {
    return std::isspace(ch);
}

bool notspace(char ch)   {
    return !std::isspace(ch);
}
+8
int main()  {
    typedef std::string::const_iterator iter;
    iter i,j;
    std::string s = "ab c";
    i = std::find_if(i, s.end(),notspace);
    j = std::find_if(i, s.end(),space);
    return 0;
}

i, . , [i, s.end()) , find_if() .

:

i = std::find_if(s.begin(), s.end(), notspace);
+5
iter i,j;
std::string s = "ab c";
i = std::find_if(i, s.end(),notspace);

std::find_if . s, .

`std::find_if(s.begin(), s.end(), notspace);

.

+3

, , , std::find_if . s.end() - , i - const_iterator, . i , . s.begin():

i = std::find_if(s.begin(), s.end(),space);

i, :

typedef std::string::iterator iter;
+3

i . -, .

    #include <string>
    #include <cctype>
    #include <algorithm>

    int main()  {
        typedef std::string::const_iterator iter;
        iter i,j;
        std::string s = "ab c";
        i = std::find_if(s.begin(), s.end(),[](char ch) { return std::isspace(ch); } );
        j = std::find_if(s.begin(), s.end(),[](char ch) { return !std::isspace(ch); } );
        return 0;
     }
+1

Source: https://habr.com/ru/post/1658837/


All Articles