Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to split an std::string into a vector<string>

Tags:

c++

string

Possible Duplicate:
How to split a string?

What is the right way to split a string into a vector of strings. Delimiter is space or comma.

like image 649
softwarematter Avatar asked Apr 09 '11 20:04

softwarematter


People also ask

How do you split a STD with string with space?

Use std::string::find and std::string::substr Functions to Split String by Space in C++ find and substr are std::string builtin functions that can be utilized to split string by any delimiter specified by the string value or a single character.

Can we take string in vector?

In your code, stringw is of the type char * and so it is not compatible with the vector you have defined.


1 Answers

A convenient way would be boost's string algorithms library.

#include <boost/algorithm/string/classification.hpp> // Include boost::for is_any_of #include <boost/algorithm/string/split.hpp> // Include for boost::split // ...  std::vector<std::string> words; std::string s; boost::split(words, s, boost::is_any_of(", "), boost::token_compress_on); 
like image 68
UncleBens Avatar answered Oct 07 '22 15:10

UncleBens