简体   繁体   English

如何在C ++中将动态2d字符串数组作为参数传递

[英]How to pass a dynamic 2d array of strings as a parameter in C++

I am trying to implement a binary tree as a 2d array. 我试图将二叉树实现为二维数组。 I want the user to enter the required height of the tree and the program should give an appropriate size array. 我希望用户输入所需的树高,程序应该给出一个合适的大小数组。 Then, I want to print the array, which is why I need to pass it as a parameter. 然后,我想打印数组,这就是我需要将其作为参数传递的原因。 However, I get the following error: 但是,我收到以下错误:

arrayTree/main.cpp|19|error: cannot convert 'std::__cxx11::string** (*)[maxNumberOfNodes] {aka std::__cxx11::basic_string<char>** (*)[maxNumberOfNodes]}' to 'std::__cxx11::string** {aka std::__cxx11::basic_string<char>**}' for argument '1' to 'void printTree(std::__cxx11::string*)'| arrayTree / main.cpp | 19 |错误:无法转换'std :: __ cxx11 :: string **(*)[maxNumberOfNodes] {aka std :: __ cxx11 :: basic_string <char> **(*)[maxNumberOfNodes]}' 'std :: __ cxx11 :: string ** {aka std :: __ cxx11 :: basic_string <char> **}'参数'1'到'void printTree(std :: __ cxx11 :: string *)'|

Please, what is causing the error and how can I fix it? 请问,导致错误的原因是什么?如何解决?

#include <iostream>
#include <string>
#include <math.h>

using namespace std;
void printTree(string** tree);
int main()
{
    int treeHeight = 0;
    int maxNumberOfNodes = 1;
    cout << "enter tree height";
    cin >> treeHeight;
    cout << treeHeight<< "\n";

    //create an array that can hold every combination for a given tree height
    maxNumberOfNodes = pow(2,treeHeight) - 1;
    string** tree [3][maxNumberOfNodes];
    cout << maxNumberOfNodes;
    printTree(tree);

}

 void printTree(string** tree){
//not fully implemented yet
    for(int i=0; i < sizeof(tree);  i++){
        cout << "*" << " ";
    }
}
string** tree [3][maxNumberOfNodes];

is the syntax of a static 2D array of type string** , where both dimensions have to be declared const. 是字符串**类型的静态2D数组的语法,其中两个维度都必须声明为const。

The difference between a static and a dynamic array is shown in here: Multidimensional variable size array in C++ 静态和动态数组之间的区别如下所示: C ++中的多维可变大小数组

Instead you want to write something like 相反,你想写类似的东西

string** tree = new string*[3];
for(int i = 0; i < 3; i++)
   tree[i] = new string[maxNumberOfNodes];

As @Remy Lebeau commented: Every occurrence of new[] needs to be answered by a delete[] call, like this: 正如@Remy Lebeau评论的那样:每次出现new[]需要通过delete[]调用来回答,如下所示:

for (int i = 0; i < 3; i++)
    delete tree[i];
delete[] tree;

to remove the dynamic allocation from the heap. 从堆中删除动态分配。

Like @drescherjm pointed out sizeof(tree) is not valid, as tree is just a pointer and does not include size information about the array. 与@drescherjm一样,指出sizeof(tree)无效,因为tree只是一个指针,不包含有关数组的大小信息。

You could solve this by additionally passing the dimensions of your array with it: 你可以通过额外传递数组的尺寸来解决这个问题:

void printTree (string** tree, int dim, int dim2)

and rewriting the loop to 并将循环重写为

for(int i = 0; i < dim; i++){
  for(int j = 0; j < dim2; j++){
    cout << tree[i][j]; //...
  }
}
 string** tree [3][maxNumberOfNodes]; 

This declares a 2D array of string** pointers. 这声明了一个string**指针的2D数组。 That is not what you want. 那不是你想要的。 You want a 2D array of string objects instead, so drop the pointers: 你想要一个string对象的2D数组,所以删除指针:

string tree [3][maxNumberOfNodes];

Also, your printTree() is not implemented correctly. 此外,您的printTree()未正确实现。 It would need to be implemented more like this instead: 它需要更像这样实现:

void printTree(string** tree, int height) {
    for(int i = 0; i < 3;  i++) {
        for(int j = 0; j < height; j++) {
            // use tree[i][j] as needed ...
        }
    }
}

That being said, since the value of maxNumberOfNodes is not known until runtime, the string tree [3][maxNumberOfNodes] syntax is declaring a Variable Length Array , which is not officially supported by the C++ standard, only as an extension by a few C++ compilers. 话虽这么说,因为maxNumberOfNodes的值直到运行maxNumberOfNodes知道,所以string tree [3][maxNumberOfNodes]语法声明一个C ++标准不正式支持的可变长度数组 ,仅作为几个C ++的扩展编译器。 You need to use new[] instead to allocate the 2nd dimension: 您需要使用new[]来分配第二维:

string* tree [3];
for(int i = 0; i < 3; ++i)
    tree[i] = new string[maxNumberOfNodes];

printTree(tree, maxNumberOfNodes);

for(int i = 0; i < 3; ++i)
    delete[] tree[i];

Or better, use std::vector instead: 或者更好的是,使用std::vector代替:

std::vector<string> tree [3];
for(int i = 0; i < 3; ++i)
    tree[i].resize(maxNumberOfNodes);

Though, in this latter case, you won't be able to pass tree to a string** function parameter, so you will have to adjust the code accordingly. 但是,在后一种情况下,您将无法将tree传递给string**函数参数,因此您必须相应地调整代码。

the method call is given by 方法调用由。给出

printTree(tree [3][maxNumberOfNodes]);

it's working for me 它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM