1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
// http://ideone.com/N0X8DO
#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';
}
}
|