Given a string of which the content could be "this is a test string jump fox string"
.
There are 2 things to note from this string - there is one space between every word, but also at a specific point there is two spaces between "jump"
and "fox"
.
I wish to push_back()
each individual word into a different index of std::vector
, but I do not want to remove whitespace that is length 2 in size. E.g. the space between "jump"
and "fox"
. Whitespace which is 1 in size can be removed.
For example, the following code:
std::stringstream m(string);
std::istream_iterator<std::string> begin(m);
std::istream_iterator<std::string> end;
std::vector<std::string> myvector(begin, end);
Places all individual words into a vector
at different indexes, but also removes the whitespace of lengths 1 and 2.
Summarised: I would like to place each word in the string into a separate index of std::vector
. There must be no whitespace after any word in any index. There must be no whitespace of length 1 in any index on its own. Only whitespace of length 2 i.e. between "jump"
and "fox"
should be placed into its own std::vector
index.
I am aware that this is a very niche question, but I am sure there must be a way around this.