C++ 字符串分割 split

来源:互联网 发布:人民法院淘宝网 编辑:程序博客网 时间:2024/04/26 14:01

字符串分割

#include <iostream>

#include <vector>
using namespace std;
std::vector<std::string> split(std::string str, std::string pattern)
{
size_t pos;
std::vector<std::string> 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()
{
string str = "name,age,time,address";
vector<string> arr;
arr = split(str, ",");
vector<string>::iterator it = arr.begin();
while (it != arr.end())
{
cout<<*it<<endl;
it++;
}
return 0;
}
0 0
原创粉丝点击