blog

枪手亨利

博客园 首页 新随笔 联系 订阅 管理

使用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

posted on 2005-11-15 15:43  henry  阅读(796)  评论(0)    收藏  举报