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

时间:2023-12-24 21:08:55

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 = ;
size_t index=s.find_first_of(delim,last);
while (index!=std::string::npos) {
ret.push_back(s.substr(last,index-last));
last=index+;
index=s.find_first_of(delim,last);
}
if (index-last>) {
ret.push_back(s.substr(last,index-last));
}
} //取vector的最后一个元素:
std::string tmp = str_arr[str_arr.size()-]; 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[] << " second: "<< svec[] << std::endl;
}
std::cout << "src string: " << str << std::endl; return ;
}