How do I split string at space and return first element? For example, in Python you would do:
string = 'hello how are you today'
ret = string.split(' ')[0]
print(ret)
'hello'
Doing this in C++, I would imagine that I would need to split the string first. Looking at this online, I have seen several long methods, but what would be the best one that works like the code above? An example for a C++ split that I found is
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace boost;
void print( vector <string> & v )
{
for (size_t n = 0; n < v.size(); n++)
cout << "\"" << v[ n ] << "\"\n";
cout << endl;
}
int main()
{
string s = "one->two->thirty-four";
vector <string> fields;
split_regex( fields, s, regex( "->" ) );
print( fields );
return 0;
}