#include <iostream>
#include <iterator>
#include <string>
#include <vector>
std::vector<std::string> split(const std::string& s, const std::string& delim)
{
std::vector<std::string> result;
std::size_t beg = 0;
std::size_t end;
while ((end = s.find(delim, beg)) != std::string::npos)
{
result.emplace_back(s.substr(beg, end - beg));
beg = end + delim.size();
}
if (beg)
result.emplace_back(s.substr(beg));
return result;
}
template <typename container_type>
void print(std::ostream& os, const container_type& container, const std::string& sep = ", ")
{
for (std::size_t i = 0; i < container.size() - 1; ++i)
os << container[i] << sep;
if (!container.empty())
os << container.back();
}
int main()
{
std::string s = "scott>=tiger>=mushroom";
std::string d = ">=";
std::cout << '"' << s << "\" split on \"" << d << "\" yields:\n";
print(std::cout, split(s,d));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDx2ZWN0b3I+CgpzdGQ6OnZlY3RvcjxzdGQ6OnN0cmluZz4gc3BsaXQoY29uc3Qgc3RkOjpzdHJpbmcmIHMsIGNvbnN0IHN0ZDo6c3RyaW5nJiBkZWxpbSkKewogICAgc3RkOjp2ZWN0b3I8c3RkOjpzdHJpbmc+IHJlc3VsdDsKCiAgICBzdGQ6OnNpemVfdCBiZWcgPSAwOwogICAgc3RkOjpzaXplX3QgZW5kOwogICAgd2hpbGUgKChlbmQgPSBzLmZpbmQoZGVsaW0sIGJlZykpICE9IHN0ZDo6c3RyaW5nOjpucG9zKQogICAgewogICAgICAgIHJlc3VsdC5lbXBsYWNlX2JhY2socy5zdWJzdHIoYmVnLCBlbmQgLSBiZWcpKTsKICAgICAgICBiZWcgPSBlbmQgKyBkZWxpbS5zaXplKCk7CiAgICB9CgogICAgaWYgKGJlZykKICAgICAgICByZXN1bHQuZW1wbGFjZV9iYWNrKHMuc3Vic3RyKGJlZykpOwoKICAgIHJldHVybiByZXN1bHQ7Cn0KCnRlbXBsYXRlIDx0eXBlbmFtZSBjb250YWluZXJfdHlwZT4Kdm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