English 中文(简体)
帮助实现更改动态数组大小的函数
原标题:Help with implementing a function to change size of dynamic array
  • 时间:2011-02-13 22:43:36
  •  标签:
  • c++

我正在尝试编写一个函数,将动态数组的大小更改为新的大小。在我的头文件中,我有:

Image **images; //pointer to a dynamic array of image pointers
int maximum; //size

我想通过分配一个新数组并在不更改其索引的情况下复制值来实现这一点。如果在newmax范围之外有非空指针,那么我们不能这样做。以下是我所拥有的:

不存在编译或运行时错误。然而,我发现新阵列的大小没有调整好。当我运行以下测试用例时:

我应该得到一个索引越界错误,但系统却让它滑动。有人看到错误了吗?我找了好几个小时,什么也找不到。

最佳回答
  images=newArray;
  for (int i =0;i<newmax;i++)
        *images[i]=*newArray[i];

这很奇怪。images和newArray现在是相同的,因此无需将newArray的内容复制回自身。所以去掉这个循环?此外,还需要添加:

 maximum = newmax;

如果1是索引,则应导致

 firstScene->addpicture("red10.bmp", 1, 13, 72);

给出一个越界错误,而目前它可能会出错?

问题回答

您应该将此功能提取到一个单独的类中,并在场景中具有该类型的数据成员:

struct ImagePtrVector {
  typedef Image *value_type;
  typedef int size_type;

  ImagePtrVector() : _begin (), _end (), _end_alloc () {}
  ~ImagePtrVector() {
    for (value_type x = _begin; x != _end; ++x) {
      delete *x;
    }        
    delete[] _begin;
  }

  // Either define these two yourself or mark private:
  ImagePtrVector(ImagePtrVector const &x);
  ImagePtrVector& operator=(ImagePtrVector const &x);

  value_type& operator[](size_type index) {
    assert(0 <= index);  // Or other checking as you like.
    assert(index < size());
    return _begin[index];
  }
  value_type const& operator[](size_type index) const {
    assert(0 <= index);  // Or other checking as you like.
    assert(index < size());
    return _begin[index];
  }

  size_type size() const { return _end - _begin; }
  size_type capacity() const { return _end_alloc - _begin; }

  void reserve(size_type capacity) {
    if (this->capacity() < capacity) {
      value_type *new_begin = new value_type[capacity];
      // Exception-safe only because I know the below won t throw.

      std::copy(_begin, _end, new_begin);
      _end_alloc = new_begin + capacity;
      _end = new_begin + this->size();
      delete[] _begin;
      _begin = new_begin;
    }
  }

  void resize(size_type size) {
    reserve(size);
    for (size_type diff = size - this->size(); diff > 0; --diff) {
      *_end++ = new Image();
    }
  }

  // Add push_back, begin/end, etc. to taste.

private:
  value_type *_begin, *_end, *_end_alloc;
};

std::vector和boost::ptr_vector的区别并非巧合,您应该评估是否真的需要编写一个特殊的容器,或者是否可以重用现有的通用容器。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签