C++ 尝试在vector类中创建resize函数

C++ 尝试在vector类中创建resize函数,c++,vector,dynamic-allocation,C++,Vector,Dynamic Allocation,我做了一个复制函数,得到了很多很好的答案,现在转到一个调整大小的函数。我正在处理的Vec类如下所示: template <class T> class Vec { public: //TYPEDEFS typedef T* iterator; typedef const T* const_iterator; typedef unsigned int size_type; //CONSTRUCTOS, ASSIGNMENT OPERATOR, & DESTRUCTOR Ve

我做了一个复制函数,得到了很多很好的答案,现在转到一个调整大小的函数。我正在处理的Vec类如下所示:

template <class T> class Vec {

public:
//TYPEDEFS
typedef T* iterator;
typedef const T* const_iterator;
typedef unsigned int size_type;

//CONSTRUCTOS, ASSIGNMENT OPERATOR, & DESTRUCTOR
Vec() {this->create(); }
Vec(size_type n, const T& t = T()) { this->create(n, t); }
Vec(const Vec& v) { copy(v); }
Vec& operator=(const Vec& v);
~Vec() { delete [] m_data; }

//MEMBER FUNCTIONS AND OTHER OPERATORS
T& operator[] (size_type i) { return m_data[i]; }
const T& operator[] (size_type i) const { return m_data[i]; }
void push_back (const T& t);
void swap(Vec<T>& left, Vec<T>& right);
iterator erase(iterator p);
void resize(size_type n, const T& fill_in_value = T());
void clear() { delete [] m_data; create(); }
bool empty() const { return m_size == 0; }
size_type size() const { return m_size; }

//ITERATOR OPERATIONS
iterator begin() { return m_data; }
const_iterator begin() const { return m_data; }
iterator end() { return m_data + m_size; }
const_iterator end() const { return m_data + m_size; }

private:
//PRIVATE MEMBER FUNCTIONS
void create();
void create(size_type n, const T& val);
void copy(const Vec<T>& v);

//REPRESENTATION
T *m_data;      //point to first location inthe allocated array
size_type m_size;   //number of elements stored in the vector
size_type m_alloc;  //number of array locations allocated, m_size <= m_alloc
};

//create an empty vector (null pointers everywhere)
template <class T> void Vec<T>::create() {
m_data = NULL;
m_size = m_alloc = 0;   //no memory allocated yet
}

//create a vector with size n, each location having the given value
template <class T> void Vec<T>::create(size_type n, const T& val) {
m_data = new T[n];
m_size = m_alloc = n;
for (T* p = m_data; p != m_data + m_size; ++p)
    *p = val;
}   

//assign one vector to another, avoiding duplicate copying
template <class T> Vec<T>& Vec<T>::operator=(const Vec<T>& v) {
Vec<T> temp = v;
swap(*this, temp);
return *this;
}

//swap one vector with another
template <class T> void Vec<T>::swap(Vec<T>& left, Vec<T>& right) {
std::swap(left.m_size, right.m_size);
std::swap(left.m_alloc, right.m_alloc);
std::swap(left.m_data, right.m_data);
}
所以我打印出p的值,它似乎为我输入的任何东西提供了合适的空间数(8位表示双精度,等等),所以从向量的开始就有了要调整大小的空间数,它们都有正确的位数。我在这里不明白的是什么

编辑:如果我将其更改为:

        m_size = m_alloc = n;
这是否意味着我正在为其余数据分配新内存?或者我应该创建一个具有新大小的临时向量,并用它替换现有向量?我真的不明白如何纠正这个问题,我不是在真正调整大小,而是在访问边界之外。到目前为止,我对它的理解是,我改变大小,在末尾向内存地址添加内容,但保持指向第一个值的指针不变(向量的名称)。这不起作用,所以我想知道我的概念误解是什么

EDIT2:我已经根据这里的答案对代码进行了更正,它是有效的:

template <class T> void Vec<T>::resize(size_type n, const T& fill_in_value) {
if (n<=m_size) {
    m_size = m_alloc = n;
}

else {
    T* new_data = new T[n];
    for (size_type i = 0; i < m_size; i++) {
        new_data[i] = m_data[i];
    }
    for (size_type i = m_size; i < n; i++) {
        new_data[i] = fill_in_value;
    }
    delete m_data;
    m_data = new_data;
    m_size = m_alloc = n;               
}           


}
template void Vec::resize(大小\类型n、常量T和填充\值){

如果(n您实际上没有为新元素分配空间。当新大小大于当前大小时,您应该分配新存储并将现有元素复制到那里

T* new_data = new T[n];
for ( size_type i = 0; i < m_size; ++i )
{
    new_data[i] = m_data[i];
}
for ( size_type i = m_size; i < n; ++i )
{
    new_data[i] = fill_in_value;
}
delete m_data;
m_data = new_data;
m_size = n;
T*new_data=new T[n];
对于(尺寸类型i=0;i
仅供参考,您应该使用placement new和raw memory storage来存储这样的内容您实际上并没有“调整大小”保存数据的数组。您只是在越界访问它。您在哪里为其他元素分配了空间?谢谢,这很有效。新的\u数据最终会在某个地方被删除吗?好的,新的\u数据会成为您的m\u数据,最终会在析构函数
~Vec()
template <class T> void Vec<T>::resize(size_type n, const T& fill_in_value) {
if (n<=m_size) {
    m_size = m_alloc = n;
}

else {
    T* new_data = new T[n];
    for (size_type i = 0; i < m_size; i++) {
        new_data[i] = m_data[i];
    }
    for (size_type i = m_size; i < n; i++) {
        new_data[i] = fill_in_value;
    }
    delete m_data;
    m_data = new_data;
    m_size = m_alloc = n;               
}           


}
T* new_data = new T[n];
for ( size_type i = 0; i < m_size; ++i )
{
    new_data[i] = m_data[i];
}
for ( size_type i = m_size; i < n; ++i )
{
    new_data[i] = fill_in_value;
}
delete m_data;
m_data = new_data;
m_size = n;