Próbuję podzielić ciąg i umieścić go w wektorzedzielenie ciąg ale utrzymanie pustych żetony C++
jednak, ja też chce mieć pusty znak, gdy istnieje kolejny separator:
Na przykład :
string mystring = "::aa;;bb;cc;;c"
Chciałbym tokenize ten ciąg na:; ograniczniki , ale pomiędzy ogranicznikami takimi jak :: i ;; Chciałbym wepchnąć mój wektor pusty ciąg;
so my desired output for this string is:
"" (empty)
aa
"" (empty)
bb
cc
"" (empty)
c
Również moim wymaganiem nie jest użycie biblioteki boost.
jeśli ktokolwiek mógł mi pomóc.
dzięki
kod który tokenize ciąg, ale nie obejmuje pustych znaków
void Tokenize(const string& str,vector<string>& tokens, const string& delim)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
próbowałeś coś? – Amit
Próbowałem powyższego kodu do tokenizacji mojego ciągu znaków, który działa, ale tylko wyklucza puste tokeny. – XDProgrammer
Dlaczego nie dodajemy 'tokens.push_back (" ");' zaraz po 'tokens.push_back (str.substr (lastPos, pos - lastPos)); '? – Bastien