c_cpp 可调整大小的数组实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 可调整大小的数组实现相关的知识,希望对你有一定的参考价值。

#include <iostream>

#include "FlexibleArray.h"

int main()
{
  FlexibleArray<char> A;
  for (unsigned int i = 0 ; i < 10 ; ++i) {
    A[i] = i + 'a';
  }

  for (unsigned int i = 0 ; i < A.Size() ; ++i) {
    std::cout << A[i] << std::endl;
  }

  FlexibleArray<int> B(10);
  for (unsigned int i = 0 ; i < 20 ; ++i) {
    B[i] = i + i;
  }

  for (unsigned int i = 0 ; i < B.Size() ; ++i) {
    std::cout << B[i] << std::endl;
  }

  FlexibleArray<int> C;
  for (unsigned int i = 5; i < 10 ; ++i) {
    C[i] = i * i;
  }

  for (unsigned int i = 0 ; i < C.Size() ; ++i) {
    std::cout << C[i] << std::endl;
  }

  FlexibleArray<int> D(5);
  D[10] = 67;
  D[20] = 97;

  for (unsigned int i = 0 ; i < D.Size() ; ++i) {
    std::cout << D[i] << std::endl;
  }

  return 0;
}
#include <cassert>  // for assert.
#include <cstdlib>  // for calloc, free.
#include <cstring>  // for memcpy.

template<class T>
class FlexibleArray
{
public:
  FlexibleArray(unsigned int aCapacity = 0)
    : mCapacity(aCapacity)
    , mSize(0)
    , mData(nullptr)
  {
    if (mCapacity) {
      Resize(mCapacity);
    }
  }

  ~FlexibleArray()
  {
    Clear();
  }

  unsigned int Size() { return mSize; }

  T& operator[] (unsigned int aIndex)
  {
    if (aIndex >= mCapacity) {
      // Resize(mCapacity && 2 * mCapacity > aIndex ? 2 * mCapacity : aIndex + 1);
      Resize(Max(2 * mCapacity, aIndex + 1));
    }
    if (aIndex >= mSize) {
      mSize = aIndex + 1;
    }
    return mData[aIndex];
  }

private:
  void Resize(unsigned int aCapacity) {
    assert(!mData || aCapacity > mCapacity);
    T* space = static_cast<T*>(calloc(1, sizeof(T) * aCapacity));
    assert(space);
    if (mData) {
      memcpy(space, mData, sizeof(T) * mCapacity);
      free(mData);
    }
    mData = space;
    mCapacity = aCapacity;
  }

  void Clear()
  {
    free(mData);
    mData = nullptr;
    mSize = 0;
    mCapacity = 0;
  }

  unsigned int Max(unsigned int a, unsigned int b)
  {
    return a > b ? a : b;
  }

  unsigned int mCapacity;
  unsigned int mSize;
  T* mData;
};
#include <iostream>

#include "AutoArray.h"

int main()
{
  AutoArray<char> A;
  for (unsigned int i = 0 ; i < 10 ; ++i) {
    A.Push(i + 'a');
  }
  for (unsigned int i = 0 ; i < A.Size() ; ++i) {
    std::cout << A[i] << std::endl;
  }
  A[3] = 'x';
  A[4] = 'y';
  A[5] = 'z';
  while (A.Size()) {
    std::cout << A.Pop() << std::endl;
  }

  AutoArray<int> B(5);
  for (unsigned int i = 0 ; i < 10 ; ++i) {
    B.Push(i);
  }
  for (unsigned int i = 0 ; i < B.Size() ; ++i) {
    std::cout << B[i] << std::endl;
  }
  // std::cout << B[20] << std::endl; // It should fail the assertion!
  while (B.Size() > 3) {
    std::cout << B.Pop() << std::endl;
  }
  // B[3] = 27; // It should fail the assertion!
  B[2] = 26;
  B[1] = 25;
  for (unsigned int i = 0 ; i < B.Size() ; ++i) {
    std::cout << B[i] << std::endl;
  }

  return 0;
}
#include <cassert>  // for assert.
#include <cstdlib>  // for calloc, free.
#include <cstring>  // for memcpy.

template<class T>
class AutoArray
{
public:
  AutoArray(unsigned int aCapacity = 0)
    : mCapacity(aCapacity)
    , mSize(0)
    , mData(nullptr)
  {
    if (mCapacity) {
      Resize(mCapacity);
    }
  }

  ~AutoArray()
  {
  }

  unsigned int Size() { return mSize; }

  void Push(T aData)
  {
    if (mSize == mCapacity) {
      // Resize(mSize ? 2 * mSize : 1);
      Resize(Max(2 * mSize, 1));
    }
    // mData[mSize] = aData;
    // ++mSize;
    mData[mSize++] = aData;
  }

  T Pop()
  {
    assert(mSize);
    // --mSize;
    // T last = mData[mSize];
    T last = mData[--mSize];
    return last;
  }

  T& operator[] (unsigned int aIndex)
  {
    assert(aIndex < mSize);
    return mData[aIndex];
  }

private:
  void Resize(unsigned int aCapacity)
  {
    assert(!mData || aCapacity > mCapacity);
    T* space = static_cast<T*>(calloc(1, sizeof(T) * aCapacity));
    if (mData) {
      memcpy(space, mData, sizeof(T) * mCapacity);
      free(mData);
    }
    mData = space;
    mCapacity = aCapacity;
  }

  void Clear()
  {
    free(mData);
    mData = nullptr;
    mSize = 0;
    mCapacity = 0;
  }

  unsigned int Max(unsigned int a, unsigned int b)
  {
    return a > b ? a : b;
  }

  unsigned int mSize;
  unsigned int mCapacity;
  T* mData;
};

以上是关于c_cpp 可调整大小的数组实现的主要内容,如果未能解决你的问题,请参考以下文章

Qt C++ 可调整大小矩形的最小尺寸

粘性标题不适用于 Primeng 中可调整大小的列。?

Jquery\ASP.NET 可拖动和可调整大小的项目列表

PyQt 中的半可调整大小的小部件

带有可调整大小的列的 Tailwind 两列布局

自动布局和可调整大小视图的问题