字符串分割函数SpliteString

字符串分割函数是程序开发中常用的函数。实现比较简单,这儿是我的实现:
using namespace std;
template<class Functor>
void __SpliteString(TCHAR* p, const TCHAR cTok, Functor func)
{
    TCHAR* e = p;
    do
    {
        e = _tcschr(p, cTok);
        if (e != NULL)
            *e++ =_T('/0');
        func(p);
        p = e;
    }while(p != NULL);
}

template<class Functor>
void SpliteString(const TCHAR* psz, const TCHAR cTok, Functor func)
{
    string str = psz;
    __SpliteString((TCHAR*)str.c_str(), cTok, func);
}

template<class Functor>
void __SpliteString(TCHAR* p, const TCHAR* szTok, Functor func)
{
    int len = _tcslen(p);
    TCHAR* e = p;
    do
    {
        e = _tcsstr(p, szTok);
        if (e != NULL)
        {
            *e =_T('/0');
            e += len;
        }
        func(p);
        p = e;
    }while(p != NULL);
}

template<class Functor>
void SpliteString(const TCHAR* psz, const TCHAR* szTok, Functor func)
{
    string str = psz;
    __SpliteString((TCHAR*)str.c_str(), szTok, func);
}

代码非常简单就不多做解释了。下面给出应用实例:
typedef vector<string> string_vector;
typedef string_vector::iterator string_iterator;

struct IntoVector
{
    IntoVector(string_vector& str_vec) : _str_vec(str_vec){}

    void operator()(const TCHAR* str)
    {
        _str_vec.push_back(str);
    }
    string_vector& _str_vec;
};

void println(const string& str)
{
    cout<<str<<endl;
}

void main()
{
    TCHAR szSource[] = "hello|this|is|a|test|string";

    string_vector splited;
    SpliteString(szSource, '|', IntoVector(splited));
    for_each(splited.begin(), splited.end(), println);
}

以上代码把字符串按照竖线分割符分割后,放入一个vector容器中,并分行打印。如果又想返回切割后字符串个数呢?改动一下:
typedef vector<string> string_vector;
typedef string_vector::iterator string_iterator;

struct IntoVector
{
    IntoVector(string_vector& str_vec) : _str_vec(str_vec),_count(0){}

    void operator()(const TCHAR* str)
    {
        _str_vec.push_back(str);
        ++ _count;
    }
    string_vector& _str_vec;
    int    _count;
};

void println(const string& str)
{
    cout<<str<<endl;
}

void main()
{
    TCHAR szSource[] = "hello|this|is|a|test|string";

    string_vector splited;
    IntoVector intovector(splited);
    SpliteString(szSource, '|', (IntoVector&)intovector);
    for_each(splited.begin(), splited.end(), println);
    cout<<intovector._count<<endl;
}

看起来很完美:为了防止拷贝重新构造IntoVector对象,特地强制类型转换为 IntoVector的引用。事实总是让人意外又恼火的,打印结果还是0。跟踪结果就可以发现SpliteString传递的还是IntoVector拷贝构造后的对象,而并不是引用。原因不是这儿探讨的范围,让我表达我也表达不清楚。不过解决办法还是有的。方法1. 把count作为外部对象引用传入:
struct IntoVector
{
    IntoVector(string_vector& str_vec,int count) : _str_vec(str_vec),_count(count){}

    void operator()(const TCHAR* str)
    {
        _str_vec.push_back(str);
        ++ _count;
    }
    string_vector& _str_vec;
    int&    _count;
};

void main()
{
    TCHAR szSource[] = "hello|this|is|a|test|string";

    string_vector splited;
    int count;
    IntoVector intovector(splited, count);
    SpliteString(szSource, '|', intovector);
    for_each(splited.begin(), splited.end(), println);
    cout<<count<<endl;
}

方法2. 当然还有更好的解决方式。boost是个好东西只需要修改一行就OK
SpliteString(szSource, '|', boost::bind<void>(boost::ref(intovector), _1));

为什么这么写?不是这儿的讨论范围了。查看一下boost源代码会很有帮助。方法3. 这个是个ugly的办法——修改SpliteString的函数,将传递的Functor改成引用:
void SpliteString(const TCHAR* psz, const TCHAR* szTok, Functor& func);
void __SpliteString(const TCHAR* psz, const TCHAR* szTok, Functor& func);

结果就是你只能使用仿函数而不能直接传递函数指针了,so ugly,所以不要考虑这种方式。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的字符串分割函数可以有多种实现方式。其中一种常见的方法是使用循环遍历字符串,根据指定的分隔符将字符串分割成多个子串。通过引用的代码示例,我们可以看到一个简单的C语言分割字符串函数实现。该代码使用了字符串的find和substr方法来进行分割操作。它将字符串分割成多个子串,并将这些子串存储在一个vector容器中。然后通过遍历该容器,将每个子串打印出来。 另一种实现方法是使用正则表达式,通过引用的代码示例,我们可以看到使用regex_token_iterator进行字符串分割的方式。该函数使用了istringstream作为输入流,以指定的分隔符为分隔符,通过getline方法将字符串分割成多个子串,并将每个子串输出。 综上所述,C语言中的字符串分割函数可以根据具体需求选择不同的实现方式,可以使用循环遍历字符串并使用字符串的find和substr方法,也可以使用正则表达式进行分割。具体的实现方式取决于项目需求和个人偏好。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++中string如何实现字符串分割函数split()——4种方法](https://blog.csdn.net/m0_58086930/article/details/122759927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值