Skip to main content
added 141 characters in body
Source Link
Jonathan Mee
  • 38.6k
  • 23
  • 140
  • 302

Both the provides us with split_view to tokenize strings, in a non-destructive manner: https://topanswers.xyz/cplusplus?q=749#a874


The previous methods cannot generate a tokenized vector in-place, meaning without abstracting them into a helper function they cannot initialize const vector<string> tokens. That functionality and the ability to accept any white-space delimiter can be harnessed using an istream_iterator. For example given: const string str{ "The quick \tbrown \nfox" } we can do this:

Both the previous methods cannot generate a tokenized vector in-place, meaning without abstracting them into a helper function they cannot initialize const vector<string> tokens. That functionality and the ability to accept any white-space delimiter can be harnessed using an istream_iterator. For example given: const string str{ "The quick \tbrown \nfox" } we can do this:

provides us with split_view to tokenize strings, in a non-destructive manner: https://topanswers.xyz/cplusplus?q=749#a874


The previous methods cannot generate a tokenized vector in-place, meaning without abstracting them into a helper function they cannot initialize const vector<string> tokens. That functionality and the ability to accept any white-space delimiter can be harnessed using an istream_iterator. For example given: const string str{ "The quick \tbrown \nfox" } we can do this:

Remove reference to Docs
Source Link
Jonathan Leffler
  • 746.6k
  • 144
  • 937
  • 1.3k

If you're interested of alternate examples of the non-hand-spun options, the last 3 options here have been further explained in documentation: https://stackoverflow.com/documentation/c%2b%2b/488/stdstring/2148/tokenize#t=201705051416058699866


If you're interested of alternate examples of the non-hand-spun options, the last 3 options here have been further explained in documentation: https://stackoverflow.com/documentation/c%2b%2b/488/stdstring/2148/tokenize#t=201705051416058699866

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Adam Pierce's answerAdam Pierce's answer provides an hand-spun tokenizer taking in a const char*. It's a bit more problematic to do with iterators because incrementing a string's end iterator is undefinedincrementing a string's end iterator is undefined. That said, given string str{ "The quick brown fox" } we can certainly accomplish this:

If you're looking to abstract complexity by using standard functionality, as On Freund suggestsOn Freund suggests strtok is a simple option:

If you're interested of alternate examples of the non-hand-spun options, the last 3 options here have been further explained in documentation: http://stackoverflow.com/documentation/c%2b%2b/488/stdstring/2148/tokenize#t=201705051416058699866https://stackoverflow.com/documentation/c%2b%2b/488/stdstring/2148/tokenize#t=201705051416058699866

Adam Pierce's answer provides an hand-spun tokenizer taking in a const char*. It's a bit more problematic to do with iterators because incrementing a string's end iterator is undefined. That said, given string str{ "The quick brown fox" } we can certainly accomplish this:

If you're looking to abstract complexity by using standard functionality, as On Freund suggests strtok is a simple option:

If you're interested of alternate examples of the non-hand-spun options, the last 3 options here have been further explained in documentation: http://stackoverflow.com/documentation/c%2b%2b/488/stdstring/2148/tokenize#t=201705051416058699866

Adam Pierce's answer provides an hand-spun tokenizer taking in a const char*. It's a bit more problematic to do with iterators because incrementing a string's end iterator is undefined. That said, given string str{ "The quick brown fox" } we can certainly accomplish this:

If you're looking to abstract complexity by using standard functionality, as On Freund suggests strtok is a simple option:

If you're interested of alternate examples of the non-hand-spun options, the last 3 options here have been further explained in documentation: https://stackoverflow.com/documentation/c%2b%2b/488/stdstring/2148/tokenize#t=201705051416058699866

added 252 characters in body
Source Link
Jonathan Mee
  • 38.6k
  • 23
  • 140
  • 302
Loading
added 1 character in body
Source Link
Jonathan Mee
  • 38.6k
  • 23
  • 140
  • 302
Loading
Rollback to Revision 1
Source Link
Jonathan Mee
  • 38.6k
  • 23
  • 140
  • 302
Loading
Let's not assume the bad practics of `using namespace std` in an answer the docs are going to point to
Source Link
Loading
Source Link
Jonathan Mee
  • 38.6k
  • 23
  • 140
  • 302
Loading