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

C++之字符串分割函数split

2015-05-04 09:48 423 查看
c++之字符串分割:

/*
*c++之字符串分割:
*/

#include <iostream>
#include <string>
#include <vector>

void split(const std::string& s, const std::string& delim,std::vector< std::string >& ret)
{
size_t last = 0;
size_t index=s.find_first_of(delim,last);
while (index!=std::string::npos) {
ret.push_back(s.substr(last,index-last));
last=index+1;
index=s.find_first_of(delim,last);
}
if (index-last>0) {
ret.push_back(s.substr(last,index-last));
}
}

//取vector的最后一个元素:
std::string tmp = str_arr[str_arr.size()-1];

int main()
{
std::string str = "test/jjksdf";
if(str.find("/") != std::string::npos){
std::vector<std::string> svec;
split(str, "/", svec);
std::cout << "first:" << svec[0] << " second: "<< svec[1] << std::endl;
}
std::cout << "src string: " << str << std::endl;

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