Hi everyone.
I read lines from a textual file with getline().
Each line is a string like:12 345 543 65
There is a way to obtain substrings with only one value, for example "345" (second column), avoiding blanks or tabs between values?
Just like cat mySTring | awk '{print $2}'
in bash scripting?
But how to avoid all those empty elements inside the vector (blanks)?
I would like to use trim() and split() boost functions, to emulate the awk syntax presented above, but I don't know how to use them, even reading the documentation...can anyone help me?
I didn't know about split, I use boost::tokenizer, but split looks simpler, so that's pretty neat.
In terms of getting rid of the extra spaces you have many options. Off the top of my head:
1) Use a regex to replace " " (2 spaces) with " " (1 space) until there are no more matches
2) If it's all numbers and whitespace, use an istringstream instead of split
3) Remove all the empty elements of the vector
I would probably go with option 2, but it does depend on your input.
If you prefer 3, the simplest implementation I can think of would be: