Find_if does not work with const_iterator

I wrote this program:

// splits a sentence into words
#include <iostream>
#include <string>
#include <algorithm>
#include "spacefunc.h"

using std::string;
using std::cout;
using std::endl;
using std::find_if;

int main() {
    typedef string::const_iterator iter;

    string input = "This is me";
    iter i = input.begin();

    while (i != input.end()) {
        iter j;

        i = find_if(i, input.end(), notspace);
        j = find_if(i, input.end(), is_space);

        cout << string(i, j) << endl;

        i = j;
    }   

    return  0;  
}

The following errors could not be completed:

word_splitter.cpp: In function ‘int main()’:
word_splitter.cpp:21:45: error: no matching function for call to ‘find_if(iter&, std::__cxx11::basic_string<char>::iterator, bool (&)(char))’
         i = find_if(i, input.end(), notspace);
                                             ^
In file included from /usr/include/c++/5/algorithm:62:0,
                 from word_splitter.cpp:4:
/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:
word_splitter.cpp:21:45: 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 = find_if(i, input.end(), notspace);
                                             ^
word_splitter.cpp:22:45: error: no matching function for call to ‘find_if(iter&, std::__cxx11::basic_string<char>::iterator, bool (&)(char))’
         j = find_if(i, input.end(), is_space);
                                             ^
In file included from /usr/include/c++/5/algorithm:62:0,
                 from word_splitter.cpp:4:
/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:
word_splitter.cpp:22:45: 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> >’)
         j = find_if(i, input.end(), is_space);

If I change the type i, jto iterator, it compiles.

What am I doing wrong since I'm sure I find_ifaccept type arguments const_iterator?

EDIT If this is a problem ibeing const_iteratorinput.end () and how iterator, why does the following code work? This is from Accelerated C++.

vector < string > split(const string & str) {
  typedef string::const_iterator iter;
  vector < string > ret;
  iter i = str.begin();
  while (i != str.end()) {
    // ignore leading blanks
    i = find_if(i, str.end(), not_space);
    // find end of next word
    iter j = find_if(i, str.end(), space);
    // copy the characters in [i, j)
    if (i != str.end())
      ret.push_back(string(i, j));
    i = j;
  }
  return ret;
}
+4
source share
3 answers

find_if the signature is as follows:

template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p);

, 2 . find_if(i, input.end(), notspace), i string::const_iterator, , input.end(), string::iterator input const. input const std::string, input.end() string::const_iterator


Post ++ 11, typedef string::const_iterator iter. auto :

string input = "This is me";
auto i = input.begin();
+7

find_if ; , , . , input.end() -const-, input const. , const iterator 'i'. const const ( const, ), input.cend().

+8

, . , , "" . : , , const, , auto .

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

bool is_space(char c) { return std::isspace(c); }
bool is_not_space(char c) { return not is_space(c); }

int main() {
  const std::string input{"This is me"};

  for (auto it = input.begin(); it != input.end();) {

    it = std::find_if(it, input.end(), is_not_space);
    auto it_end = std::find_if(it, input.end(), is_space);

    std::cout << std::string(it, it_end) << "\n";

    it = it_end;
  }
}

:

$ clang++ example.cpp -std=c++17 -Wall -Wextra
$ ./a.out 
This
is
me
+4

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


All Articles