简体   繁体   中英

List in C++ Standard Template Library (STL). I made the following program and I don't know how to make a function to print the list

    #include <iostream> 
    #include <list> 
    #include <iterator> 
    using namespace std;
    class Profesor
    {
        public:
        string nume, departament;
        int grad, vechime;
    
        Profesor(string n, string d, int g, int v);
    };

    
    Profesor::Profesor(string n, string d, int g, int v) {
        nume = n;
        departament = d;
        grad = g;
        vechime = v;
    }
    int main()
    {
        list <Profesor*> profi;
        Profesor* p;
        int opt;
        string nume, departament;
        int grad, vechime;
        do {

            cout << "1.Adaugare" << endl;
            cout << "Dati optiunea! " << endl;
            cin >> opt;
            switch (opt)
            {
            case 1:
                cout << "Nume:";
                cin >> nume;
                cout << "Departament:";
                cin >> departament;
                cout << "Grad:";
                cin >> grad;
                cout << "Vechime";
                cin >> vechime;
                p = new Profesor(nume, departament, grad, vechime);
                profi.push_front(p);
            default:
                break;
            }
        } while (opt);
        return 0;
    }

Option 1 is to add a new item into the list This is the constructor of the class So I need a function to display the entire list ajgnsjdgn afkajkf nskjfnakfakfnaf afnakfnasdnlang akfnafdakfrnaasf asdfkasfna ad akjdgnakjsgsa askfnaksd asgnaskdng asdgjnsadgag

Add a function to Profesor to output it's current variables:

void output() const {
    cout << " * nume:        " << nume        << endl;
    cout << " * departament: " << departament << endl;
    cout << " * grad:        " << grad        << endl;
    cout << " * vechime:     " << vechime     << endl;
}

Create a function that iterates through the list and calls this function.
Here is an example that uses a range based for loop:

void outputProfesors(const list<Profesor*>& profesors) {
    for (const auto& profesor : profesors) {
        profesor->output();
    }
}

Call outputProfesors() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM