Tokenize a string in C++
This post will discuss how to tokenize a string in C++.
1. Using String Stream
The common solution to tokenize a string in C++ is using std::istringstream, which is a stream class to operate on strings. The following code extract tokens from the stream using the extraction operator and insert them into a container.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <vector> #include <string> #include <sstream> #include <iterator> using namespace std; int main() { string str = "Tokenize a string in C++"; vector<string> tokens; istringstream iss(str); string s; while (iss >> s) { tokens.push_back(s); } for (const string &s: tokens) { cout << s << endl; } return 0; } |
Output:
Tokenize
a
string
in
C++
Here’s another approach to extract tokens from an input string using std::istream_iterator. The std::istream_iterator skip whitespace by default unless disabled with std::noskipws.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <string> #include <sstream> #include <iterator> using namespace std; int main() { string str = "Tokenize a string in C++"; istringstream iss(str); vector<string> tokens; copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens)); for (const string &s: tokens) { cout << s << endl; } return 0; } |
Output:
Tokenize
a
string
in
C++
The code can be further shortened to the following using the initializer list:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> #include <string> #include <sstream> #include <iterator> using namespace std; int main() { string str = "Tokenize a string in C++"; istringstream iss(str); vector<string> tokens = {istream_iterator<string>{iss}, istream_iterator<string>{}}; for (const string &s: tokens) { cout << s << endl; } return 0; } |
Output:
Tokenize
a
string
in
C++
2. Using regular expressions
Another plausible solution to tokenize a string is using a regular expression, which is the standard for performing a pattern match against a sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <string> #include <regex> using namespace std; int main() { string str = "Tokenize a string in C++"; regex reg("\\s+"); sregex_token_iterator iter(str.begin(), str.end(), reg, -1); sregex_token_iterator end; vector<string> tokens(iter, end); for (const string &s: tokens) { cout << s << endl; } return 0; } |
Output:
Tokenize
a
string
in
C++
3. Using Boost library
The Boost C++ library also offers several utility classes for this task. The Boost tokenizer class provides a view of tokens contained in a sequence by interpreting certain characters as separators.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <boost/tokenizer.hpp> using namespace std; using namespace boost; int main() { string str = "Tokenize a string in C++"; char_separator<char> sep(" "); tokenizer<char_separator<char>> tokens(str, sep); for (const string &s: tokens) { cout << s << endl; } return 0; } |
Output:
Tokenize
a
string
in
C++
That’s all about tokenizing a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)