![]() |
VOOZH | about |
In C++, splitting a string into a vector of substrings means we have to split the given string into a substring based on a given delimiter and store each substring in a vector. In this article, we will learn how to split a string into a vector of substrings in C++.
Example:
Input:
str= "Hello, I am Geek from Geeksforgeeks "
Delimiter= ' '
Output:
Hello,
I
am
Geek
from
Geeksforgeeks
To split a std::string into a std::vector of substrings use the stringstream with a combination of std::string::getline. The below steps shows how to do that:
getline to extract the substring using the delimiter.The below example demonstrates how we can split a given string into a vector of substrings in C++.
Hello, I am Geek from Geeksforgeeks
Time complexity: O(n), here n is the length of the input string.
Auxilliary Space: O(n)