The following trick using istringstream
to split a string with white spaces.
int main() {
string sentence(\"Cpp is fun\");
istringstream
Generally speaking the istringstream approach is slow/inefficient and requires at least as much memory as the string itself (what happens when you have a very large string?). The C++ String Toolkit Library (StrTk) has the following solution to your problem:
#include
#include
#include
#include "strtk.hpp"
int main()
{
std::string sentence1( "Cpp is fun" );
std::vector vec;
strtk::parse(sentence1," ",vec);
std::string sentence2( "Cpp,is|fun" );
std::deque deq;
strtk::parse(sentence2,"|,",deq);
return 0;
}
More examples can be found Here