Word split with find_if

Asked

Viewed 37 times

1

How is this string(i, j) mounted? the first string inserted in the vector is "Robison" and is correct, but to have this result the value of i should be 0 and j = 7, no?

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

using std::string;
using std::vector;
using std::cout;
using std::endl;

// predicate (split)
bool space(char c)
    { return isspace(c); }

// preticate (split)
bool not_space(char c)
    { return !isspace(c); }

vector<string> split(const string& str)
{
    typedef string::const_iterator iter;
    vector<string> ret;

    iter i = str.begin();
    while (i != str.end())
    {
        // verifica se o caracter e espaço
        i = find_if(i, str.end(), not_space);

        // procura um novo carácter
        iter j = find_if(i, str.end(), space);

        // faz a cópia dos caracteres encontrados
        if (i != str.end())
            ret.push_back(string(i, j));
        i = j;
    }
    return ret;
}

int main ()
{
    string str = "Robison Aleixo";
    split(str);

    return 0;
}

/*
 *  Basicamente as declarações acima encontram o range de uma string
    armazena as strings em um vetor "removendo" os espaços em branco
 *
 *  Exemplo: str= "Robison Aleixo"
 *
 *  iter i = str.begin();  ::::> i  será = 0
 *  while (i != str.end()) ::::> será executado até o último caracter da string
 *  i = find_if(i, str.end(), not_space);  ::::> i = 6
 *  iter j = find_if(i, str.end(), space); ::::> j = 7
 *  if i != str.end())  ::::> Verifica se não é o último caracter
 *  ret.push_back(string(i, j));  ::::> i = 6 ; j = 7
 *
 *  R o b i s o n   A l e  i  x  o
 *  0 1 2 3 4 5 6 7 8 9 10 11 12 13
 *              ^ ^
 *              i j
 *
 */

1 answer

1


i should be 0 and j = 7, no?

These are in fact the values. If we look at how the i:

i = find_if(i, str.end(), not_space /*<--not space aqui!*/);

Watch out for the not_space, soon as it starts catches the first character that is not space, and therefore the first letter, the position 0 and not the position 6 as I indicated.

Then we take the next space to the j with:

iter j = find_if(i, str.end(), space);

Now we use the predicate of space to pick up space. If we haven’t yet reached the end we use the constructor of the string which receives two iterators and starts the string with the text between them:

string(i, j)

And given in the documentation as:

template <class InputIterator> string (InputIterator first, InputIterator last);

(7) range constructor Copies the Sequence of characters in the range [first,last), in the same order.

It is important to note that the last element, the last, is not included, which works well in the algorithm because it makes the string built does not contain the space that was in j

  • Truth in the documentation itself says: Returns an iterator to the first element in the range [first,last) for which Pred Returns true. If no such element is found, the Function Returns last. I was confused, I was thinking that he was testing the predicate until false return. Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.