当前位置:学者斋 >

计算机 >C语言 >

C++的字符串分割函数的使用详解

C++的字符串分割函数的使用详解

经常碰到字符串分割的问题,下面,就和小编一起来看一看C++的字符串分割函数的使用详解,希望对大家有帮助!

C++的字符串分割函数的使用详解

  一、用strtok函数进行字符串分割

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

功能:分解字符串为一组字符串。

参数说明:str为要分解的字符串,delim为分隔符字符串。

返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。

其它:strtok函数线程不安全,可以使用strtok_r替代。

示例

//借助strtok实现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;

}

运行效果:

  二、用STL进行字符串的分割

涉及到string类的`两个函数find和substr:

1、find函数

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

功能:查找子字符串第一次出现的位置。

参数说明:str为子字符串,pos为初始查找位置。

返回值:找到的话返回第一次出现的位置,否则返回string::npos

2、substr函数

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

功能:获得子字符串。

参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)

返回值:子字符串

实现如下:

//字符串分割函数

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

{

std::string::size_type pos;

std::vectorresult;

str+=pattern;//扩展字符串以方便操作

int size=();

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

{

pos=(pattern,i);

if(pos<size)

{

std::string s=tr(i,pos-i);

_back(s);

i=pos+()-1;

}

}

return result;

}

完整代码:

/*

File :

Author : Mike

*/

#include

#include

#include

//字符串分割函数

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

{

std::string::size_type pos;

std::vectorresult;

str+=pattern;//扩展字符串以方便操作

int size=();

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

{

pos=(pattern,i);

if(pos<size)

{

std::string s=tr(i,pos-i);

_back(s);

i=pos+()-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<(); i++)

{

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

}

std::();

std::();

return 0;

}

运行效果:

三、用Boost进行字符串的分割

用boost库的正则表达式实现字符串分割

实现如下:

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

{

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

std::vectorvec;

boost::sregex_token_iterator it(n(),(),reg,-1);

boost::sregex_token_iterator end;

while(it!=end)

{

_back(*it++);

}

return vec;

}

完整代码:

//本程序实现的是利用正则表达式对字符串实现分割

//运行环境 VC6.0 + boost 库

/*

File :

Author : Mike

*/

#include

#include

#include

#include

#include "boost/"

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

{

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

std::vectorvec;

boost::sregex_token_iterator it(n(),(),reg,-1);

boost::sregex_token_iterator end;

while(it!=end)

{

_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=();i<size;i++)

{

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

}

std::();

std::();

return 0;

}

运行效果:

补充:

最近发现boost里面有自带的split的函数,如果用boost的话,还是直接用split的好,这里就不多说了,代码如下:

#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 = n(); it != (); ++ it )

cout << *it << endl;

return 0;

}

标签: 字符串 分割 函数
  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/zh-cn/jsj/cyuyan/1v9w0e.html