array_s.c

来自「C++ Primer(第三版)的随书源代码」· C语言 代码 · 共 59 行

C
59
字号
#include "Array_S.h"
#include "Array.C"

template <class elemType>
Array_Sort<elemType>::
Array_Sort( const Array_Sort<elemType> &as )
          : Array<elemType>( as )
{
    if ( as.is_dirty() )
         sort( 0, Array<elemType>::_size-1 );
    clear_bit();
}

/*******************************
// an alternative implementation
template <class elemType>
Array_Sort<elemType>::
Array_Sort( const Array_Sort<elemType> &as )
          : Array<elemType>( as )
{
    dirty_bit = as.dirty_bit;
    check_bit();
}
********************************/

template <class elemType>
void 
Array_Sort<elemType>::
grow()
{
    Array<elemType>::grow();
    sort( 0, Array<elemType>::_size-1 );
    clear_bit();
}

template <class elemType>
int 
Array_Sort<elemType>::
find( const elemType &val )
{
     int low = 0;
     int high = Array<elemType>::_size-1;
     check_bit();
     while ( low <= high ) 
     {
          int mid = ( low + high )/2;

          if ( val == _ia[ mid ])
               return mid;

          if ( val < _ia[ mid ] )
               high = mid-1;
          else low = mid+1;
     }

     return -1;
}

⌨️ 快捷键说明

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