gpt4 book ai didi

c++ - C++访问结构中的结构数组的元素

转载 作者:行者123 更新时间:2023-12-03 07:21:56 24 4
gpt4 key购买 nike

这件事使我疯狂了一段时间了。
我需要创建并遍历(后置顺序)一棵通用树,其中用户通过控制台添加每个节点(一个结构)。
我是,而不是,不允许使用STL。
用户指定将添加多少个节点,以及它可以容纳多少个“子”节点(数字)和节点名称(字符串)。
输入示例:
5
1个
2个
1℃
1天
3 E
以上意味着将添加5个节点。第一个(A)可以接受一个“子”节点,(B)可以接受2个这样的节点,而(C)可以接受1个,等等。
新添加的节点必须始终从顶部添加到“可能的”最高节点(如果它仍然可以接受新的“子”节点,如果不可能,则转到下一个)。
这个想法是创建一个数组(我知道总共将添加多少个节点),并将用户指定的那些节点放在那里,并使用结构内部的指针数组相应地“链接”它们。
给定示例的输出应为: E C D B A
我已将整个内容编写如下,但无法遍历树:
结构:

struct node {
string name = "";
int noChildrenAdded = 0;
int possibleNoChildren = 0;
int childrenFreeSlots = 0;
node* children = nullptr;
node* father = nullptr;
};
无效的遍历函数
void traverse(node* father)
{
cout << father->name << endl;
if (father == nullptr) {
return;
}
for (int i = 0; i < father->possibleNoChildren; i++) {
if (&father->children[i] == nullptr) {
continue;
}
traverse(&father->children[i]);
}
cout << father->name << "\n";
}
主要
int main() {
int n = 0;
short g = 0;
string name;
cin >> n;
node* tree = new node[n];
node* tmp = nullptr;

//adding children to tree array
for (int i = 0; i < n; i++) {
cin >> g >> name;
tree[i].possibleNoChildren = tree[i].childrenFreeSlots = g;
tree[i].name = name;
tree[i].noChildrenAdded = 0;
tree[i].children = new node[1];
}

// making connections between nodes
for (int son = 1; son < n; son++) {
for (int father = 0; father < son; father++) {
if (tree[father].childrenFreeSlots > 0) {

//resizing array
if (tree[father].noChildrenAdded == 0) {
tree[father].children[0] = tree[son];
}
else {
int added = tree[father].noChildrenAdded;
tmp = new node[added + 1];
for (int i = 0; i < added; i++) {
tmp[i] = tree[father].children[i];
}
delete[] tree[father].children;
tree[father].children = nullptr;
tree[father].children = tmp;
tree[father].children[added] = tree[son];
tmp = nullptr;
}
tree[father].noChildrenAdded++;
tree[father].childrenFreeSlots -= 1;
break;
}
}
}

//this is how it should be
cout << "Father: " << tree[1].name << "\tchildren added: " << tree[1].noChildrenAdded << endl;

//tree[0].children[0] is holding pointer to drzewo[1] so the below should give me the same answer as above.
//this is giving me wrong answer
node* ptr = &tree[0].children[0];
cout << "Father: " << ptr->name << "\tchildren added: " << ptr->noChildrenAdded << endl;

//traverse(&tree[0]);
delete[] tree;
}
问题
我无法访问结构的详细信息(例如noChildrenAdded)-尽管已填充noChildrenAdded的事实,但我却得到了零。当我通过树数组访问它时,我得到的是正确的数字,但是当我通过结构体内部的指针访问时,我得到的是0。
例:
这是正确的: cout << "Father: " << tree[1].name << "\tchildren added: " << tree[1].noChildrenAdded << endl;但这不是(尽管两者应该给出相同的数字/答案):
//tree[0].children[0] is holding pointer to tree[1] so the below should give me the same answer as above.
//this is giving me wrong answer
node* ptr = &tree[0].children[0];
cout << "Father: " << ptr->name << "\tchildren added: " << ptr->noChildrenAdded << endl;
我希望我弄乱了将子代分配给结构内部的* children数组。该名称似乎可以访问,但noChildren则不能。
两者应该给出相同的答案,但它们却不一样:
enter image description here
任何帮助将不胜感激!
PS:当我将此代码与子级静态数组一起使用时,一切正常,遍历可以正常工作,但是当我得到动态数组时,遍历就坏了。静态数组a不会这样做,因为它占用过多的内存,并且花费的时间太长,因此我的程序无法满足要求。

最佳答案

就像@ igor-tandetnik所建议的那样,使用node *指针数组解决了该问题。在我的情况下,解决方案是使用node** children而不是node *children

关于c++ - C++访问结构中的结构数组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64849024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com