C++ 如何在C+中初始化指向动态2D数组的指针+;

C++ 如何在C+中初始化指向动态2D数组的指针+;,c++,arrays,pointers,dynamic,2d,C++,Arrays,Pointers,Dynamic,2d,我给了一个冒险家类,它包含一系列函数和成员变量。 其中之一是: string*** items; 所以首先我认为这是一个3d数组,我必须制作,但是我被告知它应该是一个指向2d数组的指针 我想做的是做一个临时数组 string** temp; 初始化并填充它,然后将我的项目指向temp items = &temp; 直到函数退出为止。然后我们尝试调用项目内的索引值 cout<<*item[0][0]; 我在网上找不到任何对我有帮助的东西 如何初始化项或保留使用temp生

我给了一个冒险家类,它包含一系列函数和成员变量。 其中之一是:

string*** items;
所以首先我认为这是一个3d数组,我必须制作,但是我被告知它应该是一个指向2d数组的指针

我想做的是做一个临时数组

string** temp;
初始化并填充它,然后将我的项目指向temp

items = &temp;
直到函数退出为止。然后我们尝试调用项目内的索引值

cout<<*item[0][0];
我在网上找不到任何对我有帮助的东西

如何初始化项或保留使用temp生成的数组数据

对于那些要求代码的人,他们给了我:

class Adventurer{
private:
    string*** items;
    string name;
    double maxCarryWeight;
    double currentCarryWeight;
    int currentNumberOfItems;
    int maxNumberOfItems;
    double health;
    static int numberOfAdventurers;


public:
    Adventurer(); //default constructor
    Adventurer(const Adventurer& a);  //copy constructor
    ~Adventurer();
    bool pickUpItem(string it, double weight);
    bool dropItem(string it);
    bool dropItem(int index);
    void setName(string n);
    string getName() const;
    void setMaxCarryWeight(double w);
    double getMaxCarryWeight() const;
    void setCurrentCarryWeight(double w);
    double getCurrentCarryWeight() const;    
    void setMaxNumberOfItems(int n);
    int getMaxNumberOfItems() const;
    void setCurrentNumberOfItems(int n);
    int getCurrentNumberOfItems() const;
    int getNumberOfAdventurers() const;
    void setHealth(double h);
    double getHealth() const;
    string** getItem(int index) const;
    Adventurer& operator = (const Adventurer& a);
};

string*** items;

是指向2d数组的指针

不完全清楚您试图从您的问题中获得什么,但您的主要问题似乎与在尝试分配2d C-style
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
}
然后,您可以只传递
dynamic_array
类实例,而无需担心内存管理,而无需处理大量原始指针和它们带来的麻烦



注意:上面的
dynamic_array
类未经测试,可能需要一些调整,它也不是实现STL样式容器的好例子(您需要分配器和迭代器支持),它只是作为一个简单的
std::vector
模拟容器来绕过“无向量”任务要求。

每当你认为“动态数组”时,你的下一个想法应该是。@KeatonRoux改进你的google fu!如果你不允许使用你实际上不是在学习C++,而是尝试在文本中描述你的代码,请尝试创建一个你可以展示给我们的。这通常不是恭维。找一个更好的C++老师,而不是一个似乎相信这种语言是“C类”的人。
// 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
}
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; }
};