如何实现插入向量c++?
How to implement insert vector c++?
我必须使用 operator[]
、5 的规则、push_back
和 insert
.
来实现向量 class
template<class T>
class MyVector
{
private:
int size_;
int capacity_;
T* data;
public:
typedef T* it;
it begin();
it end();
void push_back(const T& value);
我开始创建构造函数、复制构造函数、移动构造函数、复制赋值运算符、移动赋值运算符。
我这样实现了 begin()
和 end()
:
template<class T>
typename MyVector<T>::it MyVector<T>::begin()
{
return data;
}
template<class T>
typename MyVector<T>::it MyVector<T>::end()
{
return data + size();
}
我不知道如何实现 push_back()
和 insert()
。
为了push_back()
,我想到了这样的事情:
void push_back(const T & v)
{
if (size_ < capacity_)
data [size_++] = v;
}
但是对于insert()
,我不知道。
你能帮我实现一下吗insert()
?
对于插入你必须。
- 为额外数据腾出空间。 (正如您在 push_back/emplace_back 中也需要做的那样)。
- 将当前数据从插入点移动到新的偏移量。
- 将插入的数据复制到
您的 push_back()
未正确实施。它需要在 size_
等于 capacity_
时扩大数组,以便为推送的新值腾出空间。
同样,如果需要,您的 insert()
也需要扩大阵列。然后它可以计算出新值应该去哪里的索引,将索引到数组中一个槽的所有元素 移动到 之后,然后将新值分配给该元素指数.
尝试这样的事情:
template<class T>
void MyVector<T>::grow()
{
static const int delta = ...; // <-- use whatever value makes sense for your use case...
int new_cap = capacity_ + delta;
T* new_data = new T[new_cap];
for(int i = 0; i < size_; ++i) {
new_data[i] = std::move(data[i]);
}
delete[] data;
data = new_data;
capacity_ = new_cap;
}
template<class T>
void MyVector<T>::push_back(const T & v)
{
if (size_ == capacity_)
grow();
data[size_] = v;
++size_;
}
template<class T>
void MyVector<T>::insert(typename MyVector<T>::it pos, const T & v)
{
int index = pos - data;
if (index < 0 || index > size_)
return; // or throw...
if (size_ == capacity_)
grow();
for(int i = size_ - 1; i >= index; --i)
data[i+1] = data[i];
data[index] = v;
++size_;
}
我必须使用 operator[]
、5 的规则、push_back
和 insert
.
template<class T>
class MyVector
{
private:
int size_;
int capacity_;
T* data;
public:
typedef T* it;
it begin();
it end();
void push_back(const T& value);
我开始创建构造函数、复制构造函数、移动构造函数、复制赋值运算符、移动赋值运算符。
我这样实现了 begin()
和 end()
:
template<class T>
typename MyVector<T>::it MyVector<T>::begin()
{
return data;
}
template<class T>
typename MyVector<T>::it MyVector<T>::end()
{
return data + size();
}
我不知道如何实现 push_back()
和 insert()
。
为了push_back()
,我想到了这样的事情:
void push_back(const T & v)
{
if (size_ < capacity_)
data [size_++] = v;
}
但是对于insert()
,我不知道。
你能帮我实现一下吗insert()
?
对于插入你必须。
- 为额外数据腾出空间。 (正如您在 push_back/emplace_back 中也需要做的那样)。
- 将当前数据从插入点移动到新的偏移量。
- 将插入的数据复制到
您的 push_back()
未正确实施。它需要在 size_
等于 capacity_
时扩大数组,以便为推送的新值腾出空间。
同样,如果需要,您的 insert()
也需要扩大阵列。然后它可以计算出新值应该去哪里的索引,将索引到数组中一个槽的所有元素 移动到 之后,然后将新值分配给该元素指数.
尝试这样的事情:
template<class T>
void MyVector<T>::grow()
{
static const int delta = ...; // <-- use whatever value makes sense for your use case...
int new_cap = capacity_ + delta;
T* new_data = new T[new_cap];
for(int i = 0; i < size_; ++i) {
new_data[i] = std::move(data[i]);
}
delete[] data;
data = new_data;
capacity_ = new_cap;
}
template<class T>
void MyVector<T>::push_back(const T & v)
{
if (size_ == capacity_)
grow();
data[size_] = v;
++size_;
}
template<class T>
void MyVector<T>::insert(typename MyVector<T>::it pos, const T & v)
{
int index = pos - data;
if (index < 0 || index > size_)
return; // or throw...
if (size_ == capacity_)
grow();
for(int i = size_ - 1; i >= index; --i)
data[i+1] = data[i];
data[index] = v;
++size_;
}