The code below splits a string at the delimiter and then prints each string out, so it ends up looking like the following:
scott
tiger
mushroom
I would like to understand how to go about assigning each split section into different variables. For example (split1 = scott, split2 = tiger, etc.). I have seen some ways to do this using streams, but that seems to use spaces unless I am not understanding that right. I took a shot at using arrays but not sure if that is the best option here. Any help would be appreciated.
#include <iostream>
#include <string>
#include <vector>
#include <regex>
int main()
{
const std::string text = "scott>=tiger>=mushroom";
const std::regex delimiter( ">=" ) ;
// http://en.cppreference.com/w/cpp/regex/regex_token_iterator
// note the special value of -1 for the submatch - we want the non-matched sequences of characters
// ie. iterate over the substrings that are not equal to the delimiter
const std::vector<std::string> tokens( std::sregex_token_iterator( text.begin(), text.end(), delimiter, -1 ),
std::sregex_token_iterator{} ) ;
for( constauto& tok : tokens ) std::cout << tok << '\n' ;
}