您的位置:首页 > 编程语言 > C语言/C++

C++ 字符串分割 split

2017-02-20 23:52 399 查看
字符串分割

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言