特殊文字列の文字列を分割します

std :: 文字列 test_str = " 年、月、日、HH " 
  std ::ベクトル <スタンダード:: 文字列 > RES = splitwithstr(test_str、' ' )。
  以下のためにint型 i = 0 ; i)は(res.sizeを<; iは++ 
  { 
    のstd :: COUT << RES [i]の<< はstd :: ENDL。
  } 
のstd ::ベクトル <スタンダード:: 文字列 > splitwithstr(スタンダード:: 文字列&STR、チャーCH)
{ 
  のstd :: 文字列 tmpstr = STR + CH。
  std ::ベクトル <はstd ::

文字列 > のres;
  もし(str.size()<= 0 
  { 
    戻りRES。
  } 

  size_tのPOS = tmpstr.find(CH)。
  size_tのサイズ = tmpstr.size()。
  一方、(!POS =のstd :: 文字列::のNPO)
  { 
    のstd :: 文字列 child_str = tmpstr.substr(0 、POS)。
    もし(child_str.size()> 0 
    { 
      res.push_back(child_str)。
    } 
    tmpstr = tmpstr.substr(POS + 1 )。
    POS =tmpstr.find(CH)。
  } 
  戻りRESと、
} 


int型のmain(){
     charが S [] = " 私の名前はLMMです" char * P;
    const  のchar * DELIM = "  " ; 
    P = はstrtok(S、DELIM)。
    一方、(P){ 
        COUT << P << ENDL。
        P = はstrtok(NULL、DELIM)。
    } 
 
    戻り 0 
}

 

おすすめ

転載: www.cnblogs.com/LuckCoder/p/11715592.html