C++:用于扩展数组的函数

C++: function to expand an array

本文关键字:函数 数组 扩展 用于 C++      更新时间:2023-10-16

在问这个问题之前,我研究了这个类似的问题,并试图使用它的代码来解决我的问题,但它没有起作用。

我正试图编写一个函数来扩展给定的静态对象数组。以下是目前的功能,基于上面链接问题的代码:

void MyClass::expand_array(MyClass *&old_array, int &old_array_size,
int new_slots)
{
MyClass *transfer_array = new MyClass[old_array_size + new_slots];
for (int i = 0; i < old_array_size; i++) {
transfer_array[i] = old_array[i];
}
delete[] old_array;
old_array = transfer_array;
transfer_array = NULL;
}

这甚至不编译。这是编译器的输出:

program.cpp:110:54: error: no matching function for call to ‘MyClass::expand_array(MyClass [10], int&, int)’
program.cpp:110:54: note: candidate is:
program.cpp:41:6: note: static void MyClass::expand_array(MyClass*&, int&, int)
program.cpp:41:6: note:   no known conversion for argument 1 from ‘MyClass [10]’ to ‘MyClass*&’
program.cpp:122:54: error: no matching function for call to ‘MyClass::expand_array(MyClass [10], int&, int)’
program.cpp:122:54: note: candidate is:
program.cpp:41:6: note: static void MyClass::expand_array(MyClass*&, int&, int)
program.cpp:41:6: note:   no known conversion for argument 1 from ‘MyClass [10]’ to ‘MyClass*&’
program.cpp:143:11: warning: deleting array ‘MyClass tmp_cards [10]’ [enabled by default]

(输出的最后一行来自于试图删除数组的最后一次迭代。)

您似乎正在与数组指针等价性冲突。在本文中,array指的是C阵列,例如

MyClass a[10];

在C和C++中,数组可以衰减为指针。

void f(int input[]);

该语言没有为这个函数提供任何机制来告诉它接收了多少,因此为了函数调用的目的,上面的数组会衰减到一个指针。也就是说,在上述函数的主体内,input是类型int*

这种情况也发生在其他地方,允许我们在没有数组的情况下进行指针数学运算,并在数组上进行类似指针的操作:

char a[10];
sizeof(a); // understands a is really an array and evaluates to 10
sizeof(*a); // decays a to a pointer and returns sizeof(char)
char* b = a + 1; // equivalent to b = &a[1];
b[1] = 0; // equivalent to *(b + 1) = 0;

但是,您正试图引用一个指针。在这种情况下,数组不会衰减,请考虑:

void f(int*& ptr)
{
ptr = nullptr;
}
void x()
{
int a[16];
f(a);
}

a不是指向16 int的指针,它是堆栈上的16 int。我们根本无法重新安置a

如果你需要一个可调整大小的数组,你将需要使用分配:

MyClass* array = new MyClass[10];
MyClass::expand_array(array, 10, 20);
delete [] array;

话虽如此,我想指出的是,这种方法很糟糕,因为它向消费者暴露了指针。相反,您应该将跟踪数组的大小和当前存储位置移动到您的MyClass类中。也许可以看看std::vector

但总的来说,更像这样的事情:http://ideone.com/4hgri4

#include <functional>
#include <cstdint>
#include <iostream>
// CAVEAT EMPTOR: This is a partial, naive implementation,
// it lacks copy/move constructor, operator=, iterators and
// many other things. It is solely intended to demonstrate
// internalizing the paired-state components of size and data
// to provide a discrete encapsulation of the concept of a
// dynamically sized array-like allocation.
class MyArray
{
// create an alias for the type we're using,
// this will come in handy when you learn templates.
typedef int value_type;
value_type* m_data;
size_t m_size;
public:
MyArray() : m_data(nullptr), m_size(0) {}
MyArray(size_t defaultSize) : m_data(nullptr), m_size(0)
{
resize(defaultSize);
}
void resize(size_t newSize)
{
if (newSize > size()) {
value_type* newData = new value_type[newSize];
if (m_data) {
std::copy(m_data, m_data + size(), newData);
delete [] m_data;
}
m_data = newData;
}
m_size = newSize;
if (m_size == 0 && m_data) {
delete [] m_data;
m_data = nullptr;
}
} 
value_type operator[](size_t index) const { return m_data[index]; }
value_type& operator[](size_t index) { return m_data[index]; }
size_t size() const { return m_size; }
void push_back(value_type newValue)
{
resize(m_size + 1);
m_data[size() - 1] = newValue;
}
};
int main()
{
MyArray a;
a.resize(4);
for (size_t i = 0; i < a.size(); ++i) {
a[i] = i;
}
a.push_back(10);
std::cout << "Values:n";
for (size_t i = 0; i < a.size(); ++i) {
std::cout << i << ": " << a[i] << 'n';
}
return 0;
}

有关数组指针等价的更多信息,请参阅有关它的C++常见问题部分

扩展数组的第一个参数定义为myclass*&,查看代码,我认为您需要将其设为myclass**并调整调用代码。我猜你是在声明myclass a[10],并试图传递它,但这永远不会奏效——如果你要删除它,你必须传递一个用new分配的数组。