Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有人能告诉我我的密码有什么问题吗。这段代码说明ss、se、si、arr、tree在这个函数中没有初始化_C++_Function_Data Structures_C++17_Recursive Datastructures - Fatal编程技术网

C++ 有人能告诉我我的密码有什么问题吗。这段代码说明ss、se、si、arr、tree在这个函数中没有初始化

C++ 有人能告诉我我的密码有什么问题吗。这段代码说明ss、se、si、arr、tree在这个函数中没有初始化,c++,function,data-structures,c++17,recursive-datastructures,C++,Function,Data Structures,C++17,Recursive Datastructures,以下是错误: “ss”未在此作用域中声明 “se”未在此范围内声明 未在此作用域中声明“tree” “si”未在此范围内声明 请帮我找出这个代码的错误。我不明白。我以前写过这样的函数,它们通常都能工作。但现在它不工作。请尝试以下代码: #include <bits/stdc++.h> using namespace std; int SegmentTree(int ss, int se, int si, int arr[], int &tree[]) { if (ss

以下是错误: “ss”未在此作用域中声明 “se”未在此范围内声明 未在此作用域中声明“tree” “si”未在此范围内声明
请帮我找出这个代码的错误。我不明白。我以前写过这样的函数,它们通常都能工作。但现在它不工作。

请尝试以下代码:

#include <bits/stdc++.h>

using namespace std;

int SegmentTree(int ss, int se, int si, int arr[], int &tree[]) {
  if (ss == se) {
    tree[si] = arr[ss];
    return arr[ss];
  }
  int mid = (ss + se) / 2;
  tree[si] = SegmentTree(ss, mid, ((2 * si) + 1), arr, tree) +
             SegmentTree(mid + 1, se, ((2 * si) + 2), arr, tree);
  return tree[si];
}

int main() {
  int n;
  cout << "Enter the size of the array: ";
  cin >> n;
  int arr[n];
  cout << "Enter the elements of the array: ";
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << "Our input array is:\n";
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  int tree[(4 * n)] = {0};
  cout << "\nOur output Segment tree for this array would be:\n";
  SegmentTree(0, n - 1, 0, arr, tree);
  for (int i = 0; i < (4 * n); i++) {
    cout << arr[i] << " ";
  }
  return 0;
}

int-arr[n];在标准C++中是非法的,因为n的值必须在编译时知道,而不是运行时。喜欢std::矢量arrn;而是为了这个目的。出于同样的原因,后面的树也是如此。int&tree[]不是将参数声明为对未知绑定数组的引用的方式,您需要int&tree[]或int-tree[],因为您从未修改过树。下面的错误是结果优先错误。有了有效的函数声明,它们就消失了。一般来说,如果您对错误感到困惑,您需要按照错误报告的顺序查看错误,并询问所有未初始化和未声明的错误是否都是同一回事。请解释为什么您的解决方案可以工作。前面的代码包含一些错误。树的数组引用应为int-tree[],而不是int&tree[]。另外,前面的代码在for循环中显示arr而不是树。是的,但是您必须解释这些bug是什么,以及您的代码如何解决它们。
#include<iostream>
using namespace std;

int SegmentTree(int ss, int se, int si, int arr[], int tree[]) {
  if (ss == se) {
    tree[si] = arr[ss];
    return arr[ss];
  }
  int mid = (ss + se) / 2;
  tree[si] = SegmentTree(ss, mid, ((2 * si) + 1), arr, tree) +
             SegmentTree(mid + 1, se, ((2 * si) + 2), arr, tree);
  return tree[si];
}

int main() {
  int n;
  cout << "Enter the size of the array: ";
  cin >> n;
  int arr[n];
  cout << "Enter the elements of the array: ";
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << "Our input array is:\n";
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  int tree[(4 * n)] = {};
  cout << "\nOur output Segment tree for this array would be:\n";
  SegmentTree(0, n - 1, 0, arr, tree);
  cout<<endl;
  for (int i = 0; i < (4 * n); i++) {
    cout << tree[i] << " ";
  }
  return 0;
}