分割字符串函数
//分割字符串
vector<string> FileNameSplit(string srcStr, const string& delim)
{
int nPos = 0;
vector<string> vec;
nPos = srcStr.find(delim.c_str());
while (-1 != nPos)
{
string temp = srcStr.substr(0, nPos);
vec.push_back(temp);
srcStr = srcStr.substr(nPos + 1);
nPos = srcStr.find(delim.c_str());
}
vec.push_back(srcStr);
return vec;
}
使用示例
int main() {
string iter = "TTT_2020-04-30.log.1";
vector<string> ret = FileNameSplit(iter, "_");
vector<string> yearsNum = FileNameSplit(ret[1], "-");
for (auto i = yearsNum.cbegin(); i != yearsNum.cend(); i++) {
cout << *i << endl;
}
std::cout << "Hello, World!" << std::endl;
return 0;
}