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
*
*/
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!
– Robison Aleixo