av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

C語(yǔ)言

C++的字符串分割函數(shù)的使用詳解

時(shí)間:2025-06-01 19:19:43 C語(yǔ)言 我要投稿
  • 相關(guān)推薦

C++的字符串分割函數(shù)的使用詳解

  經(jīng)常碰到字符串分割的問(wèn)題,下面,就和小編一起來(lái)看一看C++的字符串分割函數(shù)的使用詳解,希望對(duì)大家有幫助!

  一、用strtok函數(shù)進(jìn)行字符串分割

  原型: char *strtok(char *str, const char *delim);

  功能:分解字符串為一組字符串。

  參數(shù)說(shuō)明:str為要分解的字符串,delim為分隔符字符串。

  返回值:從str開頭開始的一個(gè)個(gè)被分割的串。當(dāng)沒有被分割的串時(shí)則返回NULL。

  其它:strtok函數(shù)線程不安全,可以使用strtok_r替代。

  示例:

  //借助strtok實(shí)現(xiàn)split

  #include

  #include

  int main()

  {

  char s[] = "Golden Global  View,disk * desk";

  const char *d = " ,*";

  char *p;

  p = strtok(s,d);

  while(p)

  {

  printf("%sn",p);

  p=strtok(NULL,d);

  }

  return 0;

  }

  運(yùn)行效果:

  二、用STL進(jìn)行字符串的分割

  涉及到string類的兩個(gè)函數(shù)find和substr:

  1、find函數(shù)

  原型:size_t find ( const string& str, size_t pos = 0 ) const;

  功能:查找子字符串第一次出現(xiàn)的位置。

  參數(shù)說(shuō)明:str為子字符串,pos為初始查找位置。

  返回值:找到的話返回第一次出現(xiàn)的位置,否則返回string::npos

  2、substr函數(shù)

  原型:string substr ( size_t pos = 0, size_t n = npos ) const;

  功能:獲得子字符串。

  參數(shù)說(shuō)明:pos為起始位置(默認(rèn)為0),n為結(jié)束位置(默認(rèn)為npos)

  返回值:子字符串

  實(shí)現(xiàn)如下:

  //字符串分割函數(shù)

  std::vectorsplit(std::string str,std::string pattern)

  {

  std::string::size_type pos;

  std::vectorresult;

  str+=pattern;//擴(kuò)展字符串以方便操作

  int size=str.size();

  for(int i=0; i<size; i++)

  {

  pos=str.find(pattern,i);

  if(pos<size)

  {

  std::string s=str.substr(i,pos-i);

  result.push_back(s);

  i=pos+pattern.size()-1;

  }

  }

  return result;

  }

  完整代碼:

  /*

  File  : split1.cpp

  Author : Mike

  */

  #include

  #include

  #include

  //字符串分割函數(shù)

  std::vectorsplit(std::string str,std::string pattern)

  {

  std::string::size_type pos;

  std::vectorresult;

  str+=pattern;//擴(kuò)展字符串以方便操作

  int size=str.size();

  for(int i=0; i<size; i++)

  {

  pos=str.find(pattern,i);

  if(pos<size)

  {

  std::string s=str.substr(i,pos-i);

  result.push_back(s);

  i=pos+pattern.size()-1;

  }

  }

  return result;

  }

  int main()

  {

  std::string str;

  std::cout<<"Please input str:"<<std::endl;

  //std::cin>>str;

  getline(std::cin,str);

  std::string pattern;

  std::cout<<"Please input pattern:"<<std::endl;

  //std::cin>>pattern;

  getline(std::cin,pattern);//用于獲取含空格的字符串

  std::vectorresult=split(str,pattern);

  std::cout<<"The result:"<<std::endl;

  for(int i=0; i<result.size(); i++)

  {

  std::cout<<result[i]<<std::endl;

  }

  std::cin.get();

  std::cin.get();

  return 0;

  }

  運(yùn)行效果:

  三、用Boost進(jìn)行字符串的分割

  用boost庫(kù)的正則表達(dá)式實(shí)現(xiàn)字符串分割

  實(shí)現(xiàn)如下:

  std::vectorsplit(std::string str,std::string s)

  {

  boost::regex reg(s.c_str());

  std::vectorvec;

  boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);

  boost::sregex_token_iterator end;

  while(it!=end)

  {

  vec.push_back(*it++);

  }

  return vec;

  }

  完整代碼:

  //本程序?qū)崿F(xiàn)的是利用正則表達(dá)式對(duì)字符串實(shí)現(xiàn)分割

  //運(yùn)行環(huán)境  VC6.0 + boost 庫(kù)

  /*

  File  : split2.cpp

  Author : Mike

  */

  #include

  #include

  #include

  #include

  #include "boost/regex.hpp"

  std::vectorsplit(std::string str,std::string s)

  {

  boost::regex reg(s.c_str());

  std::vectorvec;

  boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);

  boost::sregex_token_iterator end;

  while(it!=end)

  {

  vec.push_back(*it++);

  }

  return vec;

  }

  int main()

  {

  std::string str,s;

  str="sss/ddd/ggg/hh";

  s="/";

  std::vectorvec=split(str,s);

  for(int i=0,size=vec.size();i<size;i++)

  {

  std::cout<<vec[i]<<std::endl;

  }

  std::cin.get();

  std::cin.get();

  return 0;

  }

  運(yùn)行效果:

  補(bǔ)充:

  最近發(fā)現(xiàn)boost里面有自帶的split的函數(shù),如果用boost的話,還是直接用split的好,這里就不多說(shuō)了,代碼如下:

  #include

  #include

  #include

  #include

  #include

  using namespace std;

  int main()

  {

  string s = "sss/ddd,ggg";

  vectorvStr;

  boost::split( vStr, s, boost::is_any_of( ",/" ), boost::token_compress_on );

  for( vector::iterator it = vStr.begin(); it != vStr.end(); ++ it )

  cout << *it << endl;

  return 0;

  }

【C++的字符串分割函數(shù)的使用詳解】相關(guān)文章:

php字符串分割的詳解05-20

c++函數(shù)指針使用示例07-26

excel 2013函數(shù)公式使用詳解04-18

詳解PHP用substr函數(shù)截取字符串03-01

PHP常用字符串相關(guān)函數(shù)詳解03-09

詳解c語(yǔ)言字符數(shù)組與字符串的使用05-25

c語(yǔ)言字符數(shù)組與字符串的使用詳解03-02

php中利用explode函數(shù)分割字符串到數(shù)組02-20

PHP中strtotime函數(shù)使用方法詳解04-15