#include<iostream>
#include<bits/stdc++.h>
using namespace std;
vector<string> string_burst(string str,char delimeter)
{
//condition: works with only single character delimeter, like $,_,space,etc.
vector<string> words;
int n=str.length();
for(int i=0;i<n;i++)
{
int j=i;
while(str[i]!=delimeter && i<n)
i++;
string temp=str.substr(j,i-j);
words.push_back(temp);
}
return words;
}
// splits a std::string into vector<string> at a delimiter
vector<string> split(string x, char delim = ' ')
{
x += delim; //includes a delimiter at the end so last word is also read
vector<string> splitted;
string temp = "";
for (int i = 0; i < x.length(); i++)
{
if (x[i] == delim)
{
splitted.push_back(temp); //store words in "splitted" vector
temp = "";
i++;
}
temp += x[i];
}
return splitted;
}
#include <boost/algorithm/string.hpp>
std::string text = "Let me split this into words";
std::vector<std::string> results;
boost::split(results, text, [](char c){return c == ' ';});
Some of the Most Common used functions of StringStream.
clear() — flushes the stream
str() — converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.
// C++ program to understand the use of getline() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
string S, T;
getline(cin, S);
stringstream X(S);
while (getline(X, T, ' ')) {
cout << T << endl;
}
return 0;
}