How to use regex_token_iterator In C++
The regex token iterator tokenizes the sentence based on the given regular expression.
C++
// C++ Program for Conversion // String To Vector using Delimiter // Using regex_token_iterator #include <bits/stdc++.h> using namespace std; int main() { // Delimiter is " " string s = "w3wiki is a computer science portal" ; regex reg( "\\ " ); vector<string> res1( sregex_token_iterator(s.begin(), s.end(), reg, -1), sregex_token_iterator()); for ( int i = 0; i < res1.size(); i++) { cout << res1[i] << endl; } return 0; } |
Output
w3wiki is a computer science portal
C++ String to Vector Using Delimiter
Delimiters are used as separators between the characters or words in a string so that different results can get separated by the delimiter. In this article let us see the different ways in which a string with delimiters can be converted to a vector of words after getting separated by delimiters.
Example:
string x=”A B C”;
string y=”A*B*C”;
// Separate x into [‘A’,’B’,’C’] with delimiter ‘ ‘
// Separate y into [‘A’,’B’,’C’] with delimiter ‘*’
Methods that can be used to perform this operation are :
- Using find() function
- Using strtok() function
- Using getline() and stringstream
- Using find_first_not_of() with find() function
- Using regex_token_iterator