Split a string into a vector in C++
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++.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <vector> #include <algorithm> #include <sstream> #include <iterator> int main() { std::string s = "C C++ Java"; std::stringstream ss(s); std::istream_iterator<std::string> begin(ss); std::istream_iterator<std::string> end; std::vector<std::string> tokens(begin, end); for (auto &s: tokens) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
To split a string using a delimiter, we can invoke the getline() function with delimiter:
|
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 <algorithm> #include <sstream> #include <iterator> int main() { std::string s = "C,C++,Java"; std::vector<std::string> tokens; std::string token; std::stringstream ss(s); while (getline(ss, token, ',')){ tokens.push_back(token); } for (auto &s: tokens) { std::cout << s << std::endl; } return 0; } |
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:
|
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 26 27 28 |
#include <iostream> #include <vector> #include <algorithm> #include <sstream> #include <iterator> std::vector<std::string> split(std::string const &str, char delim) { std::vector<std::string> tokens; size_t start; size_t end = 0; while ((start = str.find_first_not_of(delim, end)) != std::string::npos) { end = str.find(delim, start); tokens.push_back(str.substr(start, end - start)); } return tokens; } int main() { std::string s = "C,C++,Java"; std::vector<std::string> tokens = split(s, ','); for (auto &s: tokens) { std::cout << s << std::endl; } return 0; } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/split.hpp> int main() { std::string s = "C, C++, Java"; std::vector<std::string> tokens; boost::split(tokens, s, boost::is_any_of(", "), boost::token_compress_on); for (auto &s: tokens) { std::cout << s << std::endl; } return 0; } |
Output:
C
C++
Java
That’s all about splitting a string into a vector 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 :)