您的位置:首页 > 编程语言 > C语言/C++

【转】字符串分割(C++)

2013-11-23 16:45 162 查看
原文:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/mysplitfuncpp.html

经常碰到字符串分割的问题,这里总结下,也方便我以后使用。

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

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

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

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

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

示例:

View Code

运行效果:



补充:

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

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>

using namespace std;

int main()
{
string s = "sss/ddd,ggg";
vector<string> vStr;
boost::split( vStr, s, boost::is_any_of( ",/" ), boost::token_compress_on );
for( vector<string>::iterator it = vStr.begin(); it != vStr.end(); ++ it )
cout << *it << endl;
return 0;
}


好,就这些了,希望对你有帮助。

E-Mail : Mike_Zhang@live.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: