Tokenize a string: Difference between revisions
Content added Content deleted
(Adds slope example) |
|||
Line 1,364: | Line 1,364: | ||
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")) |
copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ".")) |
||
std::cout << '\n'; |
std::cout << '\n'; |
||
}</syntaxhighlight> |
|||
{{works with|C++23}} |
|||
C++20 and C++23 drastically improve the ergonomics of simple manipulation of ranges. |
|||
<syntaxhighlight lang="cpp">#include <string> |
|||
#include <ranges> |
|||
int main() { |
|||
std::string s = "Hello,How,Are,You,Today"; |
|||
s = s // Assign the final string back to the string variable |
|||
| std::views::split(',') // Produce a range of the comma separated words |
|||
| std::views::join_with('.') // Concatenate the words into a single range of characters, joined with periods |
|||
| std::ranges::to<std::string>(); // Convert the range of characters into a regular string |
|||
std::cout << s; |
|||
}</syntaxhighlight> |
}</syntaxhighlight> |
||