Assuming you have a string composed of words separated by semicolons (";"), you aim to split this string into a vector of separate words.
To achieve this, you can leverage the standard library function std::getline. It allows you to read data from a string stream, treating it as a sequence of lines. By defining a delimiter, you can instruct std::getline to split the string into substrings based on that delimiter.
Here's a sample code demonstrating how to do this:
#include <sstream> #include <iostream> #include <vector> using namespace std; int main() { vector<string> strings; istringstream f("denmark;sweden;india;us"); string s; while (getline(f, s, ';')) { cout << s << endl; strings.push_back(s); } return 0; }
In this code:
Within the while loop:
This approach provides a simple and efficient way to split a string using a specified token, such as ";".
The above is the detailed content of How to Split a C std::string Using Tokens (';')?. For more information, please follow other related articles on the PHP Chinese website!