About spaces and commas

I have the following code, but I have got the feeling that the line beginning with 'numofspaces +=' does not work as it should be. It should add the number of spaces of the iterator 'spaces' to the previous number of spaces of 'spaces'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::regex rgx("\\s+");
std::sregex_token_iterator endOfSequence;
std::sregex_token_iterator token(strnl.begin(), strnl.end(), rgx, -1);
std::regex rgxspaces("+");
std::sregex_token_iterator spaces(strnl.begin(), strnl.end(), rgxspaces, -1);
while(token != endOfSequence && spaces != endOfSequence)
{
    spacesstr = spaces->str();
    numofspaces += std::count(spacesstr.begin(), spacesstr.end(), ' ') ;
    commas += std::count(strnlnext.begin(), strnlnext.begin() + numofspaces, ',');
    commas += std::count(strnlnext.begin(), strnlnext.begin() + numofspaces, '|');
    commas += std::count(strnlnext.begin(), strnlnext.begin() + numofspaces, ']');
    token++;
    spaces++;
}


The lines I am trying to read in are:
1
2
 +     +            +  
[Bb,Bb,C,Db|Eb,F,Gb,Ab]

in which
 
 +     +            +  

is strnl
and
 
[Bb,Bb,C,Db|Eb,F,Gb,Ab]

is strnlnext.

So after the first space (spacesstr = 1 + 1 = 2) commas must be 0.
After the next couple of spaces (spacesstr = 1 + 5 + 1 = 7) commas must be 2.
After the next couple of spaces (spacesstr = 1 + 5 + 12 + 1 = 19) commas must be 7. Does someone see the solution?
Last edited on
Topic archived. No new replies allowed.