This post will discuss how to split a string into a vector in C++.

1. Using String Stream

A simple solution to split a space-separated std::string into a std::vector<string> is using string streams. This can be implemented as follows in C++.

Download  Run Code

Output:

C
C++
Java

 
To split a string using a delimiter, we can invoke the getline() function with delimiter:

Download  Run Code

Output:

C
C++
Java

2. Using string::find_first_not_of

We can use a combination of string::find_first_not_of and string::find functions to split a string in C++ into a vector. The following C++ program demonstrates its usage:

Download  Run Code

Output:

C
C++
Java

3. Using Boost

Another good alternative is to use boost’s string algorithms library. It contains the boost::split function to splits the input into parts separated by a delimiter.

Download Code

Output:

C
C++
Java

That’s all about splitting a string into a vector in C++.