• 您的位置我爱Aspx >> VC.Net >> <b>正确地调用字符串相关的函数(第一部分)</b>
  • <b>正确地调用字符串相关的函数(第一部分)</b>

  • 作者:aspxer  来源:internet  日期:2007-5-21 23:13:59  关键字:
  • 正确地调用字符串相关的函数(第一部分)

    即便在常规的程序开发过程中,程序员也常常会碰到调用字符串函数方面的困难。注意,这里的字符串函数是指有关字符指针操作相关的函数。例如,我们现有这么一个函数:reverse_string:

    template&lt; class CharType&gt;

    void reverse_string( const CharType * strDest)

    {

    int nLength = /* what should we call here? */;

    int idxFirst = 0;

    int idxLast = nLength - 1;

    while ( idxFirst &lt; idxLast)

    {

    std::swap( strDest[ idxFirst], strDest[ idxLast];

    ++ idxFirst;

    -- idxLast;

    )

    }

    在/* what should we call here? */位置,我们使用strlen来调用const char *字符串,使用wcslen来调用const wchar_t *字符串。

    解决方法是建立一个t_strlen函数,如下所示:

    template&lt; class CharType&gt;

    int t_strlen( const CharType * str)

    {

    return strlen( str);

    }

    int t_strlen( const wchar_t * str)

    {

    return wcslen( str);

    }

    我对这篇文章有话说?
  • 广告位招租,广告代号:content_468_15
  • 上一篇:<b>正确地调用字符串相关的函数(第二部分)</b>
    下一篇:<b>从赋值运算符的默认实现中获益(第二部分)</b>
  • 相关文章