简单的变长数组

以下是实现以及应用的一个例程:

#include <iostream>

using namespace std;

template <typename Object>
class Vector
{
    public:
        explicit Vector( int initSize = 0 )
        : theSize( initSize ),theCapacity( initSize + SPARE_CAPACITY )
        { objects = new Object[ theCapacity ]; }
        Vector( const Vector &rhs ) : objects( NULL )               //三大函数
        { operator=( rhs ); }
        ~Vector()
        { delete [] objects; }

        const Vector &operator=( const Vector &rhs )
        {
            if ( this != &rhs ){
                delete [] objects;
                theSize = rhs.size();
                theCapacity = rhs.theCapacity;

                objects = new Object[ capacity() ];
                for ( int k = 0 ; k < size() ; k++ )
                    objects[k] = rhs.objects[k];
            }

            return *this;
        }

        void resize( int newSize )                                  //重设大小
        {
            if ( newSize > theCapacity )
                reserve( newSize * 2 + 1 );
            theSize = newSize;
        }

        void reserve( int newCapacity )
        {
            if ( newCapacity < theSize )
                return ;

            Object *OldArray = objects;

            objects = new Object[ newCapacity ];
            for ( int k = 0 ; k < theSize ; k++ )
                objects[k] = OldArray[k];

            theCapacity = newCapacity;

            delete [] OldArray;
        }

        Object &operator[]( int index )                             //下标引用
        { return objects[ index ]; }
        const Object &operator[]( int index ) const
        { return objects[ index ]; }

        bool empty() const
        { return size() == 0; }

        int size() const
        { return theSize; }
        int capacity() const
        { return theCapacity; }

        void push_back( const Object &x )
        {
            if ( theSize == theCapacity )
                reserve( 2 * theCapacity + 1 );
            objects[theSize++] = x;
        }

        void pop_back()
        { theSize--; }

        const Object &back() const
        { return objects[theSize-1]; }

        typedef Object *iterator;
        typedef const Object *const_iterator;

        iterator begin()
        { return &objects[0]; }
        const_iterator begin() const
        { return &objects[0]; }
        iterator end()
        { return &objects[ size() ]; }
        const_iterator end() const
        { return &objects[ size() ]; }

        enum { SPARE_CAPACITY = 16 };

    private:
        int theSize;
        int theCapacity;
        Object *objects;
};

int main()
{
    Vector<int> Num;
    cout << "输入一组数据:" << endl;

    int temp;
    while ( cin >> temp )
        Num.push_back( temp );

    cout << "你输入的数据是:" << endl;

    Vector<int>::iterator iter = Num.begin();
    for ( ; iter != Num.end() ; ++iter )
        cout << *iter << " ";

    cout << endl;

    return 0;
}
时间: 2024-12-20 15:46:06

简单的变长数组的相关文章

C 语言变长数组 struct 中 char data[0] 的用法

1.结构体内存布局(padding) 为了让CPU能够更舒服地访问到变量,struct中的各成员变量的存储地址有一套对齐的机制.这个机制概括起来有两点:第一,每个成员变量的首地址,必须是它的类型的对齐值的整数倍,如果不满足,它与前一个成员变量之间要填充(padding)一些无意义的字节来满足:第二,整个struct的大小,必须是该struct中所有成员的类型中对齐值最大者的整数倍,如果不满足,在最后一个成员后面填充. The following typical alignments are va

变长数组 - 转

http://ericwang.github.io/program/2010/02/10/c_Variable_length_arrays/ C中的Variable length arrays (变长数组) Variable length arrays 是C99的特性,而不是 C++98 的,关于c99标准的变长数组, 在标准的6.7.5.2 Array declarators里面有这样的说明: 2.Only ordinary identifiers (as defined in 6.2.3)

有关变长数组(VLA)(转)

原链接:http://blog.chinaunix.net/uid-24347760-id-1989578.html 在突然听到这个名词后,在网上搜的. 1. 变长数组是分配在堆栈上的, 其实从语义的角度也应该是这样, 变长数组还是一个数组, 还是一个局部变量, 在c语言中, 局部变量是分配在堆栈上的, malloc才是分配在堆上面的. 这里面没有不存在什么 内存泄漏, 因为堆栈上的内存是不需要程序员管理的   2. 变长数组和其他变量共同存在于一个作用域之内时, 变长数组是在最后分配的, 也就

变长数组(variable-length array,VLA)

处理二维数组的函数有一处可能不太容易理解,数组的行可以在函数调用的时候传递,但是数组的列却只能被预置在函数内部.例如下面这样的定义: 1 #define COLS 4 2 int sum3d(int ar[][COLS], int rows) 3 { 4 int r, c, tot; 5 tot = 0; 6 7 for(r = 0; r < rows; r++) 8 for(c = 0; c < COLS; c++) 9 tot += ar[r][c]; 10 return tot; 11

变长数组_相乘取结果

//变长数组 相乘取结果 #include <stdio.h> int main(void){ // int array_01[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; int array_02[4][3] = {12,11,10,9,8,7,6,5,4,3,2,1}; int result[3][3] = {0}; int i, j, k; for (i = 0; i < 3; i ++){ //遍历array_01数组元素 for (j = 0;j

C之变长数组

变长数组是C99标准新加入的一个特性,它的加入大大方便了我们的编程,所谓变长数组,不是数组的长度可变,而是指允许使用变量来定义数组.这可以使我们写出更具通用性的函数.下面是一个例子,函数sum2d完成将一个二位数组中的所有数值相加并返回其和. #include<stdio.h> #define SIZE 10 #define LOC 2 #define ROW 4int sum2d(int loc, int row, int num[loc][row]); int main(void){ in

C99新增内容之变长数组(VLA)

我们在使用多维数组是有一点,任何情况下只能省略第一维的长度.比如在函数中要传一个数组时,数组的行可以在函数调用时传递,当属数组的列却只能在能被预置在函数内部.看下面一个例子: #define COLS 4 int sum2d(int ar[][COLS],int rows) { int r; int c; int tot=0; for(r=0;r<rows;r++) for(c=0;c<COLS;c++) tot+=ar[r][c]; return tot; } 现在假设定义了如下数组: in

(变长数组)变量也可做特殊数组的长度

这个问题困扰我好久,终于完美区分: 看一个例子: main() { int n=10; int a[n]; scanf("%d",&a[2]); printf("%d",a[2]); system("pause"); } 以上例子中,n明显是一个整型的变量,虽然付了值,但是他仍然不可做为数组的大小,按理说是编译不通过的,但有些编译器,却让着个编译过了, 我们说:因为定义数组时,分配空间是需要一个固定的值,来确定你所申请的空间的大小. 若i

变长数组与函数参数

代码: #include <stdio.h> #include <stdlib.h> void setval(const size_t, const size_t, int arr[*][*]); void display(const size_t, const size_t, int arr[*][*]); int main(void) { const size_t ROWS = 5; const size_t COLS = 3; int arr[ROWS][COLS]; set