gpt4 book ai didi

c++ - 关于指向指针数组的指针

转载 作者:行者123 更新时间:2023-11-30 03:20:25 25 4
gpt4 key购买 nike

我目前正在做一项作业,我想我已经成功地制作了一个从文件中读取的元素周期表中元素的链表(元素的数量会有所不同)。

但我现在正在尝试创建一个指向元素指针数组的指针(Element **ptr = new Element *[n] 在主文件中找到并传递到 read_table).我不确定我应该怎么做。我做的对吗?还是应该是 ptr[i] -> *head.pElement

元素结构已在另一个文件中创建,表格将成为该文件中的原型(prototype)。

struct Node {
Element *pElement;
Node *next;
};

int table(Element **ptr) { // Was originally Element **&ptr, but that might have been supposed to be for my prototype
Node *head = new Node; // starts off the linked list
Node *temp = new Node; // temp node to make switch for head node
Element e;

int counter = 0; // counter to keep track of num of elements

// open input file
ifstream infile;
infile.open(file_path_will_be_placed_here);

// loop to read file and create linked list
while(infile >> e.atomicNumber) {
infile >> e.name;
infile >> e.abbreviation;
infile >> e.mass;

head -> pElement = new Element; // the node's pElement points to a new Element
*head -> pElement = e; // sets node's pElement to the read data stored in e
*temp -> next = head; // might have to point to &head
head = temp; // head points to where temp does
temp = new Node; // temp points to new node

counter++; // increment counter every time for every element
}

for(int i = 0; i < counter; i++) {
// confused !@!@?
ptr[i] -> head.pElement;
}

最佳答案

while(infile >> e.atomicNumber) {
infile >> e.name;
infile >> e.abbreviation;
infile >> e.mass;

首先,您希望从 infile 中提取的所有内容都成功以保持循环运行:

while (infile >> e.atomicNumber >> e.name >> e.abbreviation >> infile >> e.mass) {

然后让我们看看您的“列表”:

    head->pElement = new Element;  // ok
*head->pElement = e; // ok
*temp->next = head;
head = temp; // now you have a head with a pElement pointing to garbage
// and a next pointing to the node with pElement pointing
// to the Element you just read, the next of that Node
// points to garbage, though *)
temp = new Node;

counter++;
}

*) 因此,当您尝试遍历列表时,您将不得不忽略 head 的 pElement 但也不知道何时到达列表结束,因为您从未设置 next -指向 nullptr 的指针,因此它可以从指向下一个节点的指针中区分出来。

int table(Element **&ptr) // should return std::size_t
{
ifstream infile{ "test.txt" };
int num_elements{}; // should be std::size_t
Element e;
Node *head{};
Node *current{};
while (infile >> e.atomicNumber >> e.name >> e.abbreviation >> e.mass) {

if (!head) {
head = new Node;
head->next = nullptr;
head->pElement = new Element;
*head->pElement = e;
current = head;
}
else {
current->next = new Node;
current = current->next;
current->next = nullptr;
current->pElement = new Element;
*current->pElement = e;
}
++num_elements;
}

Element **array = new Element*[num_elements];

current = head;
Node *temp;
for (int i = 0; i < num_elements && current; ++i) {
array[i] = current->pElement;
temp = current;
current = current->next;
delete temp;
}

ptr = array;
return num_elements;
}

int main()
{
Element **elements;
int num_elements = table(elements);

// do something with elements

for(int i{}; i < num_elements; ++i)
delete elements[i];
delete [] elements;
}


真正的tm解决方案:

#include <vector>
#include <string>
#include <iterator>
#include <fstream>

struct Element {
int atomicNumber;
std::string name;
std::string abbreviation;
double mass;
};

std::istream& operator>>(std::istream& is, Element &element)
{
Element e;
if (!(is >> e.atomicNumber >> e.name >> e.abbreviation >> e.mass))
return is;
element = e;
return is;
}

std::vector<Element> read_elements()
{
std::ifstream infile{ "test.txt" };
std::vector<Element> elements{ std::istream_iterator<Element>{ infile },
std::istream_iterator<Element>{} };
return elements;
}

关于c++ - 关于指向指针数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52911918/

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