#include <iostream>
#include <string>
#include <vector>
bool check(std::size_t pos)
{
return pos != std::string::npos;
}
std::vector<std::string> tokenize(const std::string& text, const std::string& delimiters = " ")
{
std::vector<std::string> tokens;
std::size_t start_pos;
std::size_t end_pos = 0;
while (check(start_pos = text.find_first_not_of(delimiters, end_pos)) )
{
end_pos = text.find_first_of(delimiters, start_pos);
tokens.emplace_back(text.substr(start_pos, end_pos - start_pos));
}
return tokens;
}
int main()
{
{
auto t = tokenize("8 (4 ounce) fillets salmon,1/2 cup peanut oil,4 tablespoons soy sauce,"
"4 tablespoons balsamic vinegar,4 tablespoons green onions (chopped),3 teaspoons "
"brown sugar,2 cloves garlic (minced),1 1/2 teaspoons ground ginger,2 teaspoons "
"crushed red pepper flakes,1 teaspoon sesame oil,1/2 teaspoon salt", ",");
for (auto& token : t)
std::cout << token << '\n';
std::cout << '\n';
}
{
auto t = tokenize("Test 1,string 1,Test 2,string 2:Test 3:string 3", ",:");
for (auto& token : t)
std::cout << token << '\n';
std::cout << '\n';
}
{
auto t = tokenize("Mary had a little lamb");
for (auto& token : t)
std::cout << token << '\n';
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKYm9vbCBjaGVjayhzdGQ6OnNpemVfdCBwb3MpCnsKICAgIHJldHVybiBwb3MgIT0gc3RkOjpzdHJpbmc6Om5wb3M7Cn0KCnN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiB0b2tlbml6ZShjb25zdCBzdGQ6OnN0cmluZyYgdGV4dCwgY29uc3Qgc3RkOjpzdHJpbmcmIGRlbGltaXRlcnMgPSAiICIpCnsKICAgIHN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiB0b2tlbnM7CgogICAgc3RkOjpzaXplX3Qgc3RhcnRfcG9zOwogICAgc3RkOjpzaXplX3QgZW5kX3BvcyA9IDA7CgogICAgd2hpbGUgKGNoZWNrKHN0YXJ0X3BvcyA9I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