目的:类似于java的split函数实现字符串根据"|"分割得到的字串
1.加上头文件
#include<vector>
2.复制本地文件可调用的函数代码
/*
aim:便于以后可以根据分割符扩展压缩文件数量author:wsh
time:20180403
input:
string strTime:需要分割的字符串
output:
vector<WCHAR*>:分割后保存的vector容器
*/
static std::vector<WCHAR*> split(WCHAR* strTime)
{
std::vector<WCHAR*> result;
int pos = 0;//字符串结束标记,方便将最后一个单词入vector
size_t i;
for( i = 0; i < wcslen(strTime); i++)
{
if(strTime[i] == '|')
{
WCHAR* temp = new WCHAR[i-pos+1];
memset(temp,0x00,sizeof(WCHAR));
wcsncpy(temp,strTime+pos,i-pos);
temp[i-pos] = 0x00;
result.push_back(temp);
pos=i+1;
}
}
//判断最后一个
if (pos < i)
{
WCHAR* temp = new WCHAR[i-pos+1];
memset(temp,0x00,sizeof(WCHAR));
wcsncpy(temp,strTime+pos,i-pos);
temp[i-pos] = 0x00;
result.push_back(temp);
}
return result;
}
3.调用示例
std::vector<WCHAR*> vec = split(L"xiaoming|xiaohong"); //测试分割效果
for (std::vector<WCHAR*>::const_iterator itr = vec.cbegin();itr!=vec.cend();itr++)//输出
wprintf(L"%s\n",*itr);
vec.erase(vec.begin(),vec.end());//***回收内存***//
4.结果图