#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> split_string(const std::string& str,
const std::string& delimiter)
{
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos)
{
strings.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
}
int main()
{
auto strings = split_string("foo\nbar\nthis\nworks", "\n");
int i = 1;
for (auto itr = strings.begin(); itr != strings.end(); itr++)
std::cout << "#" << i++ << " - \"" << *itr << "\"\n";
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKc3RkOjp2ZWN0b3I8c3RkOjpzdHJpbmc+IHNwbGl0X3N0cmluZyhjb25zdCBzdGQ6OnN0cmluZyYgc3RyLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvbnN0IHN0ZDo6c3RyaW5nJiBkZWxpbWl0ZXIpCnsKICAgIHN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiBzdHJpbmdzOwoKICAgIHN0ZDo6c3RyaW5nOjpzaXplX3R5cGUgcG9zID0gMDsKICAgIHN0ZDo6c3RyaW5nOjpzaXplX3R5cGUgcHJldiA9IDA7CiAgICB3aGlsZSAoKHBvcyA9IHN0ci5maW5kKGRlbGltaXRlciwgcHJldikpICE9IHN0ZDo6c3RyaW5nOjpucG9zKQogICAgewogICAgICAgIHN0cmluZ3MucHVzaF9iYWNrKHN0ci5zdWJzdHIocHJldiwgcG9zIC0gcHJldikpOwogICAgICAgIHByZXYgPSBwb3MgKyAxOwogICAgfQoKICAgIC8vIFRvIGdldCB0aGUgbGFzdCBzdWJzdHJpbmcgKG9yIG9ubHksIGlmIGRlbGltaXRlciBpcyBub3QgZm91bmQpCiAgICBzdHJpbmdzLnB1c2hfYmFjayhzdHIuc3Vic3RyKHByZXYpKTsKCiAgIC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