尚不完全清楚您试图从您的问题中实现什么,但似乎您的主要问题可能与在尝试分配 2D C 样式 std::string 数组时返回局部变量的地址有关。下面是一个非常基本的示例,说明如何通过返回分配的 2D 数组然后获取此返回值的地址并将其存储在您的 std::string*** items 变量中来避免此类问题。
// allocate memory for a 2D C-style array of std::string's
std::string** allocate_2d_array(std::size_t rows, std::size_t cols) {
std::string** items_arr = new std::string*[rows];
for (std::size_t i = 0; i < rows; ++i)
items_arr[i] = new std::string[cols];
return items_arr;
}
// print each element of the 2D C-style array via a pointer to the array
void print_items(std::ostream& os, std::string*** items, std::size_t rows, std::size_t cols) {
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < cols; ++j)
os << (*items)[i][j] << ' ';
os << '\n';
}
}
// destruct the 2D C-style array
void deallocate_2d_array(std::string** items_arr, std::size_t rows, std::size_t cols) {
for (std::size_t i = 0; i < rows; ++i)
delete[] items_arr[i];
delete[] items_arr;
}
int main(void) {
std::size_t rows = 3; // matrix rows
std::size_t cols = 3; // matrix columns
// allocate a 2D array of std::string's
std::string** items_arr = allocate_2d_array(items, 3, 3);
// set the pointer to a 2D std::string array to address of items_arr
std::string*** items = &items_arr;
int count = 0;
// fill items_arr with data via items pointer
for (std::size_t i = 0; i < rows; ++i) {
for (std::size_t j = 0; j < cols; ++j)
(*items)[i][j] = std::to_string(++count);
}
print_items(std::cout, items); // print matrix to terminal
deallocate_2d_array(items_arr, rows, cols); // deallocate items_arr
}
然而,正如 cmets 中所提到的,这与现代 c++ 不符,人们宁愿使用 std::vector<std::vector<std::string>> 来存储 std::string 实例的矩阵。
您提到使用std::vector 不是一种选择,但我怀疑您的老师可能没有说过要制作与std::vector 具有相似语义的自己的准系统动态数组,所以这始终是解决这些愚蠢问题的一种方法限制。考虑到这一点,下面是一个非常基本(未经测试)的类的框架,它模仿 std::vector(不使用分配器),这将使您的任务更加简单。
template<typename Ty>
class dynamic_array {
public:
typedef Ty value_type;
typedef Ty& reference;
typedef const Ty& const_reference;
typedef Ty* pointer;
typedef const Ty* const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// CONSTRUCTION/ASSIGNMENT
dynamic_array()
: arr_capacity(0), arr_size(0) {}
dynamic_array(size_type count)
: arr_capacity(count), arr_size(count) { allocate(count); }
~dynamic_array() { destroy(); }
dynamic_array& operator=(dynamic_array _other) {
swap(*this, _other);
return *this;
}
// CAPACITY
bool empty() const noexcept { return arr_size; }
size_type size() const noexcept { return arr_size; }
size_type capacity() const noexcept { return arr_capacity; }
void reserve(size_type new_cap) { if (new_cap > arr_capacity) reallocate(new_cap); }
// ELEMENT ACCESS
reference operator[](size_type n) { return arr[n]; }
const_reference operator[](size_type n) const { return arr[n]; }
// MODIFIERS
void clear() {
for (size_type i = 0; i < arr_size; ++i)
(&arr[i])->~value_type();
arr_size = 0;
}
void push_back(const value_type& _val) {
if (arr_size == arr_capacity) // TODO: expand arr using reallocate
pointer val = new (arr + arr_size) value_type(_val);
++arr_size;
}
void pop_back() {
(&arr[arr_size-1])->~value_type();
--arr_size;
}
void swap(dynamic_array& _other) {
std::swap(arr, _other.arr);
std::swap(arr_capacity, _other.arr_capacity);
std::swap(arr_size, _other.arr_size);
}
static void swap(dynamic_array& lhs, dynamic_array& rhs) { lhs.swap(rhs); }
private:
value_type* arr;
size_type arr_capacity;
size_type arr_size;
void allocate(size_type n) { arr = new value_type[n]; }
void reallocate(size_type new_cap) {
value_type* tmp = new value_type[new_cap];
size_type tmp_rows = (new_cap > arr_capacity) ? arr_capacity : new_cap;
for (size_type i = 0; i < tmp_rows; ++i)
tmp[i] = std::move(arr[i]);
delete[] arr;
arr = tmp;
arr_capacity = new_cap;
}
void destroy { clear(); delete[] arr; }
};
然后,您不必担心大量原始指针和它们带来的麻烦,而是可以传递 dynamic_array<dynamic_array<std::string>> 类实例,而无需担心内存管理。
注意:上面的 dynamic_array 类未经测试,可能需要一些调整,它也不是实现 STL 样式容器的好例子(你需要分配器和迭代器支持),它只是旨在作为准系统 std::vector 模仿容器来绕过“无向量”任务要求。