We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The unique algorithim removes consecutive duplicates. The added in predicate function is saying "remove dups if 1 of them is a space and so is the other". This has the effect of moving each character forward in the string by however many were erased and returns a "new end" iterator (which points to the end of the new, shorter string). There are some "junk chars" at the end of the original string, so erase is called to clear these. The next while loop is removing any spaces from the end of the string (since they would only have been removed by unique if they were consecutive duplicates).
Then we set the delimiter to a space and use it to search for the individual words in the string. Using this delimiter, substrings are added to the "splits" vector of strings. The string::npos is the greatest value and means that it has reached the end of the string. So the min of this value or length of string (it will be length of string, because it is smaller) minus the most-recent starting point (i), will give you the index of the last char in string (which is also the last char of the last word). However, substr takes the length (including the starting char) of the substr as its second argument, so we add the +1 so that it will include the last char (since strings are zero indexed).
Cats and a Mouse
You are viewing a single comment's thread. Return to all comments →
The unique algorithim removes consecutive duplicates. The added in predicate function is saying "remove dups if 1 of them is a space and so is the other". This has the effect of moving each character forward in the string by however many were erased and returns a "new end" iterator (which points to the end of the new, shorter string). There are some "junk chars" at the end of the original string, so erase is called to clear these. The next while loop is removing any spaces from the end of the string (since they would only have been removed by unique if they were consecutive duplicates).
Then we set the delimiter to a space and use it to search for the individual words in the string. Using this delimiter, substrings are added to the "splits" vector of strings. The string::npos is the greatest value and means that it has reached the end of the string. So the min of this value or length of string (it will be length of string, because it is smaller) minus the most-recent starting point (i), will give you the index of the last char in string (which is also the last char of the last word). However, substr takes the length (including the starting char) of the substr as its second argument, so we add the +1 so that it will include the last char (since strings are zero indexed).