如何从 C++ 中的列表中推送值和删除记录?

How to push values and delete a record from a list in C++?

我是 C++ 中数据结构的新手,我遇到了一些无法修复的问题,我想使用 STL 列表 显示以下内容:

Input for data radius:
Radius 1: 20
Press [Y] for next input: Y
Radius 2: 12
Press [Y] for next input: N
    
List of Existing Records:
ID:1, Radius: 20, Volume: 33,514.67
ID:2, Radius: 12, Volume: 7,239.17
Total record: 2

Would you like to remove specific data [Press Y for Yes]: Y
Enter record ID: 1

List of Existing Records:
ID:2, Radius: 12, Volume: 7,239.17
Total record: 1

我写了这样的代码,我想将球体推到列表中,我希望用户选择要删除的记录,但我不太确定正确的方法:

#include <iostream>
#include <list>

using namespace std;

struct sphere {
    int recordID;
    double radius, volume;
};

double dataVolume(double r) {
    double v = (4 * 3.14 * r * r * r) / 3.0;
    return v;
}

void dataRadius(sphere* values) {
    int i = 0;
    char choice;
    do {
        cout << "Radius " <<i+1 <<": ";
        cin >> values->radius;
        values->volume = dataVolume(values->radius);
        cout << "Press [Y] for next input: ";
        cin >> choice;
        i++;
    } while (choice == 'Y');

    Record.push_back(values); //why i cant push back the values to the list
}

void displayData(list<sphere>Record) {
    
    cout << "List of Existing Records:" << endl;
    list<int>::iterator i;
    int count = 0;
    for (auto i = Record.begin(); i != Record.end(); i++) {
        cout << "ID: " << count + 1 << ", Radius: " << i->radius <<
            ", Volume: " << i->volume << endl;
        count = count + 1;
    }
    cout << "Total record: " << count << endl;
}

void deleteData(list<sphere>Record) {
    char cho;
    cout << "Would you like to remove specific data [Press Y for Yes]: ";
    cin >> cho;
    if (cho == 'Y') { 
        int id;
        cout << "Enter record ID: ";
        cin >> id;
        Record.erase(id); // why it shows error
    }
}


int main() {

    list<sphere>Record;
    sphere values;

    dataRadius(&values); 

    displayData(Record);

    deleteData(Record);

   displayData(Record);
    return 0;
}

你犯了一些错误。在删除元素时,不能只使用整数 index.U 必须使用列表类型的迭代器。然后你是 push_backing 记录列表中的球体元素,在所有选择之后只插入用户的最后选择。我也修好了。第三个错误是,你是 push_backing 指向球体 v 的指针,但从技术上讲,我们必须推送该指针的值。我也修好了。现在它工作正常。

#include <iostream>
#include <list>

using namespace std;

struct sphere {
    int recordID;
    double radius, volume;
};
list<sphere>Record; //Fixed:- made this global
double dataVolume(double r) {
    double v = (4 * 3.14 * r * r * r) / 3.0;
    return v;
}

void dataRadius(sphere* values) {
    int i = 0;
    char choice;
    do {
        cout << "Radius " <<i+1 <<": ";
        cin >> values->radius;
        values->volume = dataVolume(values->radius);
        Record.push_back(*values);//Fixed:- inserting value not address and get it inside scope so that all values are inserted.
        cout << "Press [Y] for next input: ";
        cin >> choice;
        i++;
    } while (choice == 'Y');

}

void displayData(list<sphere> &Record) {//Fixed:- passed as referemce
    
    cout << "List of Existing Records:" << endl;
    list<int>::iterator i;
    int count = 0;
    for (auto i = Record.begin(); i != Record.end(); i++) {
        cout << "ID: " << count + 1 << ", Radius: " << i->radius <<
            ", Volume: " << i->volume << endl;
        count = count + 1;
    }
    cout << "Total record: " << count << endl;
}

void deleteData(list<sphere> &Record) { //Fixed:- passed as reference
    char cho;
    list<sphere>::iterator itr1, itr2;
        itr2 = Record.begin();
        itr1 = Record.begin();
    cout << "Would you like to remove specific data [Press Y for Yes]: ";
    cin >> cho;
    if (cho == 'Y') { 
        int id;
        cout << "Enter record ID: ";
        cin >> id;
        advance(itr1, id-1);
        Record.erase(itr1);//Fixed:- using iterator of same type as list to erase element
    }
}


int main() {

    sphere values;

    dataRadius(&values); 

    displayData(Record);

    deleteData(Record);
    displayData(Record);
    return 0;
}