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;
}
#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++:实现split分割字符串
- C++ split 字符串分割实现
- C++字符串分割函数split实现
- [C/C++标准库]_[初级]_[分割字符串Split]
- C++ 分割字符串-Split
- How to split string in C++(分割字符串)
- C/C++实现split分割字符串
- C++:实现split分割字符串
- C++ 字符串分割方法 实现split
- C++:实现split分割字符串
- C++:实现split分割字符串
- C++:实现split分割字符串 - 改进
- 在C++中实现字符串分割--split
- c++ string split分割字符串
- c++ 读取txt文件,split分割字符串
- C++:实现split分割字符串
- C++之split字符串分割
- C++之字符串分割函数split
- [C/C++标准库]_[初级]_[分割字符串Split]
- C++:实现split分割字符串