c++ split 拆分字符串实现 作者:马育民 • 2022-03-06 15:17 • 阅读:10107 # 方式一 实现 ``` #include #include #include //字符串分割函数 std::vector split(std::string str, std::string pattern) { std::string::size_type pos; std::vector result; str += pattern;//扩展字符串以方便操作 int size = str.size(); for (int i = 0; i < size; i++) { pos = str.find(pattern, i); if (pos < size) { std::string s = str.substr(i, pos - i); result.push_back(s); i = pos + pattern.size() - 1; } } return result; } ``` 测试 ``` int main() { std::string str; std::cout<<"Please input str:"<>str; getline(std::cin,str); std::string pattern; std::cout<<"Please input pattern:"<>pattern; getline(std::cin,pattern);//用于获取含空格的字符串 std::vector result=split(str,pattern); std::cout<<"The result:"< 原文出处:http://malaoshi.top/show_1IX2tefNI8f9.html