⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstl_basic_string.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
📖 第 1 页 / 共 3 页
字号:
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::rfind( const char_type* str,
                                               size_type pos,
                                               size_type count ) const
{
    if( count > size() )
        return npos;

    size_type end_pos = size() - 1;
    if( end_pos > pos )
        end_pos = pos;

    for( ++end_pos; end_pos-- > 0; )
    {
        if( traits_type::eq( data()[end_pos], *str )
            && traits_type::compare( data() + end_pos, str, count ) == 0 )
            return end_pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::rfind( char_type c,
                                               size_type pos ) const
{
    if( size() < 1 )
        return npos;

    size_type end_pos = size() - 1;
    if( end_pos > pos )
        end_pos = pos;

    for( ++end_pos; end_pos-- > 0; )
    {
        if( traits_type::eq( data()[end_pos], c ) )
            return end_pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_first_of( const char_type* str,
                                                       size_type pos,
                                                       size_type count ) const
{
    for( ; pos < size(); ++pos )
    {
        if( find_char( str, data()[pos], 0, count ) != npos )
            return pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_last_of( const char_type* str,
                                                      size_type pos,
                                                      size_type count ) const
{
    if( size() == 0 )
        return npos;

    size_type end_pos = size() - 1;
    if( end_pos > pos )
        end_pos = pos;

    for( ++end_pos; end_pos-- > 0; )
    {
        if( find_char( str, data()[end_pos], 0, count ) != npos )
            return end_pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_first_not_of( const char_type* str,
                                                           size_type pos,
                                                           size_type count ) const
{
    for( ; pos < size(); ++pos )
    {
        if( find_char( str, data()[pos], 0, count ) == npos )
            return pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_first_not_of( char_type c,
                                                           size_type pos ) const
{
    for( ; pos < size(); ++pos )
    {
        if( !traits_type::eq( data()[pos], c ) )
            return pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_last_not_of( const char_type* str,
                                                          size_type pos,
                                                          size_type count ) const
{
    if( size() == 0 )
        return npos;

    size_type end_pos = size() - 1;
    if( end_pos > pos )
        end_pos = pos;

    for( ++end_pos; end_pos-- > 0; )
    {
        if( find_char( str, data()[end_pos], 0, count ) == npos )
            return end_pos;
    }
    return npos;
}
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_last_not_of( char_type c,
                                                          size_type pos ) const
{
    if( size() < 1 )
        return npos;

    size_type end_pos = size() - 1;
    if( end_pos > pos )
        end_pos = pos;

    for( ++end_pos; end_pos-- > 0; )
    {
        if( !traits_type::eq( data()[end_pos], c ) )
            return end_pos;
    }
    return npos;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline void swap( basic_string<CharT, Traits, Allocator>& lhs,
                  basic_string<CharT, Traits, Allocator>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

//以下为重载的运算符

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs,
           const basic_string<CharT, Traits, Allocator>& rhs )
{
    basic_string<CharT, Traits, Allocator> str( lhs );
    str.append( rhs );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const char* lhs, const basic_string<CharT, Traits, Allocator>& rhs )
{
    basic_string<CharT, Traits, Allocator> str( lhs );
    str.append( rhs );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs, const char* rhs )
{
    basic_string<CharT, Traits, Allocator> str( rhs );
    str.append( lhs );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs, CharT c )
{
    basic_string<CharT, Traits, Allocator> str( lhs );
    str.append( 1, c );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+(  CharT c, const basic_string<CharT, Traits, Allocator>& rhs )
{
    basic_string<CharT, Traits, Allocator> str( rhs );
    str.append( 1, c );
    return str;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline bool operator==( const basic_string<CharT, Traits, Allocator>& lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( lhs.compare(rhs) == 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator==( const basic_string<CharT, Traits, Allocator>& lhs,
                        const CharT* rhs )
{
    return ( lhs.compare(rhs) == 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator==( const CharT* lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( rhs.compare(lhs) == 0 );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline bool operator!=( const basic_string<CharT, Traits, Allocator>& lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( lhs.compare(rhs) != 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator!=( const basic_string<CharT, Traits, Allocator>& lhs,
                        const CharT* rhs )
{
    return ( lhs.compare(rhs) != 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator!=( const CharT* lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( rhs.compare(lhs) != 0 );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline bool operator<( const basic_string<CharT, Traits, Allocator>& lhs,
                       const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( lhs.compare(rhs) < 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator<( const basic_string<CharT, Traits, Allocator>& lhs,
                       const CharT* rhs )
{
    return ( lhs.compare(rhs) < 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator<( const CharT* lhs,
                       const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( rhs.compare(lhs) > 0 );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const basic_string<CharT, Traits, Allocator>& lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( lhs.compare(rhs) <= 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const basic_string<CharT, Traits, Allocator>& lhs,
                        const CharT* rhs )
{
    return ( lhs.compare(rhs) <= 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const CharT* lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( rhs.compare(lhs) >= 0 );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const basic_string<CharT, Traits, Allocator>& lhs,
                       const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( lhs.compare(rhs) > 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const basic_string<CharT, Traits, Allocator>& lhs,
                       const CharT* rhs )
{
    return ( lhs.compare(rhs) > 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const CharT* lhs,
                       const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( rhs.compare(lhs) < 0 );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const basic_string<CharT, Traits, Allocator>& lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( lhs.compare(rhs) >= 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const basic_string<CharT, Traits, Allocator>& lhs,
                        const CharT* rhs )
{
    return ( lhs.compare(rhs) >= 0 );
}

template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const CharT* lhs,
                        const basic_string<CharT, Traits, Allocator>& rhs )
{
    return ( rhs.compare(lhs) <= 0 );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -