C++ Code Sample: Split string by space

C++ Code Sample: Split string by space

Mẫu code sample dưới đây sử dụng để tách chuỗi cách nhau bởi dấu space

 

#include <vector>
#include <string>
std::vector<std::string> SplitBySpace(std::string str)
{
    std::vector<std::string> list;
    std::string word = "";
    for (auto x : str)
    {
        if (x == ' ')
        {
            list.push_back(word);
            word = "";
        }
        else
        {
            word = word + x;
        }
    }

    return list;
}

END

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.