使用stl 拆分字符串
搞了一上午,才弄明白 find_first_of 的用法, 弱~~~~~~~~
find_first_of
功能: 查找包含子串中的任何字符,返回第一个位置
函数原型
1) size_type find_first_of(const basic_string& s, size_type pos = 0)
2) size_type find_first_of(const charT* s, size_type pos, size_type n)
3) size_type find_first_of(const charT* s, size_type pos = 0)
4) size_type find_first_of(charT c, size_type pos = 0)
它的功有特别多,比如利用它可以实现字符串过滤功能,这里我就不说了,网上有好多例子。
vSplitString 函数的功能是拆分字符串
void vSplitString( string strSrc ,vector<string>& vecDest ,char cSeparator )
{
if( strSrc.empty() )
return ;
string::size_type size_pos = 0;
string::size_type size_prev_pos = 0;
while( (size_pos = strSrc.find_first_of( cSeparator ,size_pos)) != string::npos)
{
string strTemp= strSrc.substr(size_prev_pos , size_pos-size_prev_pos );
vecDest.push_back(strTemp);
cout << "string = "<< strTemp << endl;
size_prev_pos =++ size_pos;
}
vecDest.push_back(&strSrc[size_prev_pos]);
cout << "end string = "<< &strSrc[size_prev_pos] << endl;
};
参考 : C++ primet
浙公网安备 33010602011771号