简单的顺序存储容器

本文简述了一个实现了顺序存储的容器类(Array),并对其进行了验证。

此 Array 类具有如下能力:

  • 支持泛型
  • 指定存储容量
  • 可用列表(initializer_list)初始化
  • 支持拷贝和移动语义
  • 可按值或引用传递此类的对象,也可作为值返回
  • 可用下标运算符访问元素
  • 支持基于范围的循环

◆ 实现

使用类模板,支持泛型(array.hxx),

template <class _T>
class Array
{...

    size_t
    __size__;


    shared_ptr<_T>
    __buffer__;

...

在构造函数中,指定存储容量,

Array(size_t size = 0)
    : __size__(size), __buffer__(nullptr)
{...
}

在构造函数中,指定列表(initializer_list)初始化,

Array(initializer_list<_T> init)
    : __size__(init.size()), __buffer__(nullptr)
{...
}

实现拷贝、移动构造函数与赋值函数,可按值或引用传递该类的对象,也可作为值返回,

Array(Array const& other)
    : __size__(other.__size__), __buffer__(other.__buffer__)
{
}


Array(Array && other)
    : __size__(other.__size__), __buffer__(other.__buffer__)
{...
}


Array&
operator=(Array const& other)
{...
}


Array&
operator=(Array && other)
{...
}

可用下标运算符访问,

_T const&
operator[](size_t idx) const
{...
}


_T &
operator[](size_t idx)
{...
}

实现 begin()、end() 函数以支持基于范围的循环,

_T const*
begin() const
{...
}


_T *
begin()
{...
}


_T const*
end() const
{...
}


_T *
end()
{...
}

◆ 逻辑

以下类图展现了 Array 类的主要逻辑结构。

class

◆ 验证

为了验证此类满足的能力,设计了如下的测试用例(test_array.cpp),

static
void
test_array_default_constructor()
{
    std::printf("\n%70s\n", "[test_array_default_constructor]");
    Array<int> a(3);
    a[0] = 10, a[1] = 11, a[2] = 12, a[3] = 13;
    std::printf("\na[0] = %d, a[2] = %d\n", a[0], a[2]);
}


static
void
test_array_initializer_constructor()
{
    std::printf("\n%70s\n", "[test_array_initializer_constructor]");
    Array<char> a({'a','b','c','d'});
    std::printf("\na[1] = %c, a[3] = %c\n", a[1], a[3]);
}


static
void
test_array_copy_constructor()
{
    std::printf("\n%70s\n", "[test_array_copy_constructor]");
    Array<char> a({'x','y','z'});
    Array<char> b(a);
    Array<int> c ;
    c = a_new_array();
    std::printf("\na[0] = %c, a[2] = %c\n", a[0], a[2]);
    std::printf("\nb[0] = %c, b[2] = %c\n", b[0], b[2]);
    std::printf("\nc[0] = %d, c[2] = %d\n", c[0], c[2]);
}


static
void
test_array_move_constructor()
{
    std::printf("\n%70s\n", "[test_array_move_constructor]");
    Array<int> a(5);
    a[0] = 0, a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4;
    Array<int> b = std::move(a);
    //std::printf("\na[0] = %d, a[4] = %d\n", a[0], a[4]);
    std::printf("\nb[0] = %d, b[4] = %d\n", b[0], b[4]);
}


static
void
test_array_copy_assign_operator()
{
    std::printf("\n%70s\n", "[test_array_copy_assign_operator]");
    Array<int> a(5);
    a[0] = 0, a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4;
    Array<int> b(5);
    b = a;
    std::printf("\na[1] = %d, a[4] = %d\n", a[1], a[4]);
    std::printf("\nb[1] = %d, b[4] = %d\n", b[1], b[4]);
}


static
void
test_array_move_assign_operator()
{
    std::printf("\n%70s\n", "[test_array_move_assign_operator]");
    Array<int> a(5);
    a[0] = 0, a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4;
    Array<int> b(5);
    b = std::move(a);
    //std::printf("\na[1] = %d, a[4] = %d\n", a[1], a[4]);
    std::printf("\nb[1] = %d, b[4] = %d\n", b[1], b[4]);
}


static
void
test_array_shared()
{
    std::printf("\n%70s\n", "[test_array_shared]");
    Array<char> a({'a','b','c','d'});
    {
        Array<char> b(a);
        b[1] = 'x';
        Array<char> c(b);
        c[3] = 'z';
    }
    std::printf("\na[1] = %c, a[2] = %c, a[3] = %c\n", a[1], a[2], a[3]);
}



static
void
test_array_rangedfor()
{
    std::printf("\n%70s\n", "[test_array_rangedfor]");
    Array<char> a({'a','b','c','d'});
    for (char x : a)
        std::printf("%2c", x);
    std::printf("\n");
}

编译代码(-std=c++11)成功后运行可执行文件。以下是执行过程中的部分输出,

                                      [test_array_default_constructor]
a[0] = 10, a[2] = 12


                                  [test_array_initializer_constructor]
a[1] = b, a[3] = d


                                         [test_array_copy_constructor]
a[0] = x, a[2] = z
b[0] = x, b[2] = z
c[0] = 10, c[2] = 12


                                         [test_array_move_constructor]
b[0] = 0, b[4] = 4


                                     [test_array_copy_assign_operator]
a[1] = 1, a[4] = 4
b[1] = 1, b[4] = 4


                                     [test_array_move_assign_operator]
b[1] = 1, b[4] = 4


                                                   [test_array_shared]
a[1] = x, a[2] = c, a[3] = z


                                                [test_array_rangedfor]
 a b c d

◆ 最后

完整的代码请参考 [gitee] cnblogs/17964979

posted @ 2024-01-15 17:54  green-cnblogs  阅读(12)  评论(0编辑  收藏  举报