代码未在联机编译器上显示结果

code not showing result on online compiler

本文关键字:显示 结果 编译器 联机 代码      更新时间:2023-10-16

我试图为树的垂直顺序遍历编写代码,我的代码在 code::blocks 上打印结果,但是当在 geekforgeeks 在线 ide 中运行相同的东西时,它不会打印结果。为什么要这样做?

void getvert(Node* root,int hd,map<int,vector<int>>m){
if(root==NULL)return;
m[hd].push_back(root->data);
getvert(root->left,hd-1,m);
getvert(root->right,hd+1,m);
}

void verticalOrder(Node *root)
{
map<int,vector<int>>m;
int hd=0;
getvert(root,hd,m);
auto it=m.begin();
for(;it!=m.end();it++)
{
for (int i=0; i<it->second.size(); ++i)
cout<<it->second[i];
cout<<endl;
}
}

函数getvert接受最后一个参数m作为值。在函数中对它所做的更改将针对对象的本地副本进行。因此,您看不到任何更改verticalOrder.

更改getvert,使其接受m作为引用。

void getvert(Node* root,int hd, map<int,vector<int>>& m) // Need reference argument
{
...
}