array.cpp

来自「C语言实战105例/王为青, 陈圣亮编著 北京-人民邮电出版社 2007」· C++ 代码 · 共 47 行

CPP
47
字号
/*------------------------------------------------------------------------*/
/*                                                                        */
/*  ARRAY.CPP                                                             */
/*                                                                        */
/*  Copyright Borland International 1991                                  */
/*  All Rights Reserved                                                   */
/*                                                                        */
/*------------------------------------------------------------------------*/

#if !defined( CHECKS_H )
#include <Checks.h>
#endif  // CHECKS_H

#if !defined( __ARRAY_H )
#include <Array.h>
#endif  // __ARRAY_H

void Array::add( Object& toAdd )
{
    lastElementIndex++;
    while( ptrAt( lastElementIndex ) != ZERO &&
           lastElementIndex <= upperbound
         )
        lastElementIndex++;

    if( lastElementIndex > upperbound )
        reallocate( lastElementIndex - lowerbound + 1 );

    setData( lastElementIndex, &toAdd );
    itemsInContainer++;
    CHECK( itemsInContainer > 0 );
}

void Array::addAt( Object& toAdd, int atIndex )
{
    PRECONDITION( atIndex >= lowerbound );
    if( atIndex > upperbound )
        reallocate( atIndex - lowerbound + 1 );

    if( ptrAt( atIndex ) != ZERO )
        {
        delete ptrAt( atIndex );
        }

    setData( atIndex, &toAdd );
}

⌨️ 快捷键说明

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