-1

我需要一些我认为很简单的帮助。我可以将学生分配给一个项目。但是当我删除项目时,学生仍然保留项目名称。我正在考虑将其重命名为“无”,但我不知道该怎么做。帮助?

编辑 map<int, Student> mstorevector<int> storeid添加。

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
class Human {
public:
    virtual void print() const = 0;
};

class Student : public Human {
protected:
    string studname;
    int studId;
    string project;
public:

    Student();
    Student (string studname, int studId) : studname("Unknown"), studId(0), project("None") 
    {
        cout << "A student is created: Name = " << studname
         << ". Id = " << studId << endl;
    }

    virtual void print() const {
    cout << "Name = " << studname << ". Id = " << studId << ". Project = " << project <<endl; }

    void setSName (string sname) { studname = sname; }
    void setSID (int sID) { studId = sID; }
    void printStudentInfo() const;
    void printStudentInfoline() const;
};

void Student::printStudentInfo() const
{
    cout << "\nStudent name: " << studname << endl;
    cout << "Student ID: " << studId << endl;
    cout << "Project: " << project << endl;
}

void Student::printStudentInfoline() const
{
    cout << studId << ", " << studname << ", " << project << endl;
}

class Project {
protected:
    string projname;

public:
    vector <Student> students;
    vector <int> storeid;

    Project (string projname) : projname(projname) { cout << "Project " << projname << " created" << endl;}

    void setPName (string projname) { this->projname = projname; }

    void add (int& sid) 
    {
        //student.setProject (projname);
        storeid.push_back(sid);
    }

    int returnid(int& a)
    {   
        return storeid[a]; 
    }

    int returnsize()
    {   return storeid.size(); }

    void printproj() const {
        cout << endl << projname << " list: \n";
        cout << "Student(s) : " << endl;
        for (int i = 0; i < storeid.size(); i++){
            cout << storeid[i] << endl;
        }
    }

    void printprojname() const {
    cout << projname << endl;
    }


};

int main() {
string StudentName;
string ProjectName;

int Studentid;

Student *s1;
Project *p1;

vector<Student> store;
vector<Project> projstore;
map<int, Student> mstore;


map<int, Student>::const_iterator itr;
    for (int n=0; n<3; n++) //loop to create 3 students
   { 
    cout <<"Enter name : ";
    getline(cin, StudentName);
    cout <<"Enter  ID : ";
    cin >> Studentid;

    s1 = new Student(StudentName, Studentid);
    s1->setSName(StudentName);
    s1->setSID(Studentid);
    store.push_back(*s1);
    mstore.insert(make_pair(Studentid, *s1));
    cin.get();
    }
    //print map
    for(itr=mstore.begin(); itr!=mstore.end() ;++itr)
    itr->second.printStudentInfo();

    //itr=mstore.begin()+2;
    //itr.print();




cout << "Enter project name: ";
getline(cin, ProjectName);

p1 = new Project(ProjectName);
p1->setPName(ProjectName);

 //Assigning student to project
    cout << endl;
    cout << "How many students? :" ;

    int y;
    cin >> y;
    for ( int i = 0; i < y; i++){
    cout << "Who would you like to add to this project?" << endl;
    int x = 1;

    for(itr=mstore.begin(); itr!=mstore.end() ;++itr)
    itr->second.printStudentInfoline();

    int insID;
    cout << "Enter ID number: ";
    cin >> insID;
    p1->add(insID);

    /*
    for ( it = store.begin(); it != store.end(); ++it ) {
        // For each friend, print out their info

    cout << x << ". ";
          it->printStudentInfoline();
    x++;

    }
    x = 1;  
    int insS;
    cout << "Enter number: ";
    cin >> insS;
    p1->add(store[(insS-1)]);   //stores selected student into the object
    */

    cout << "\nAdding Student done\n" << endl;
    }
    projstore.push_back(*p1);

    //Mstore finds for related ids and displays them accordingly
cout << "print project"<< endl;
    vector<Project>::iterator pt;
    for ( pt = projstore.begin(); pt != projstore.end(); ++pt ) {
        pt->returnsize();
        for (int i=0; i <pt->returnsize(); i++){
        cout << pt->returnid(i) << endl;

        itr=mstore.find(pt->returnid(i));

        itr->second.printStudentInfo();
        }            
    }


    cout << endl;
    cout << "Deleting project" << endl;
    cout << "What would you like to remove?" << endl;
    int x = 1;

    //storeid will display ids. How do I link them to `store` map?
    for ( pt = projstore.begin(); pt != projstore.end(); ++pt ) {
      cout << x << ". ";
      pt->printprojname();
      x++;
    }


    //Now to delete the selected project
    int delP;
    cout << "Enter number: ";
    cin >> delP;
    cin.ignore();
    system("pause");
    projstore.erase(projstore.begin()+delP-1);

// Students

cout << "\n Current students" << endl;
for(itr=mstore.begin(); itr!=mstore.end() ;++itr)
    itr->second.printStudentInfo();

}
4

1 回答 1

1

查看如何将学生添加到项目中:

void add (Student& student)
{
  student.setProject (projname);
  students.push_back (student);    // <-- AHA!
}

首先将项目名称分配给学生,然后项目存储学生的副本。此后,Project 与原来的 Student 没有联系,也无法在时机成熟时通知他/她自己的消亡。

您将不得不重新考虑这种设计;有三个主要选项:1)学生可以在商店中查找他们各自的项目,2)项目可以在向量中查找其学生students,或 3)项目拥有学生(在这种情况下,他们可能应该是研究生)。

编辑:
如果这是您想要的方式,请map<int, Student> store使用 ID 号作为索引来存储学生。然后一个项目可以有一个vector<int>(或set<int>)个学生证号码。它可以轻松地在商店中查找学生。

编辑:
要打印出整个学生集合:

for(map<int, Student>::const_iterator itr=store.begin(); itr!=store.end() ;++itr)
  itr->second.print();

编辑:
如果项目有一个vector<int>学生证号码,那么你认为Project::add(?)应该采取什么论据?

编辑:
该项目可以通过学生 ID 号和访问权限对学生进行操作mstore

// in Project:
mstore[id].whatever()

编辑:
有时提出正确的问题——或者在这种情况下,正确地表达问题——是成功的一半。'现在如何将“无”更改为插入的项目名称?一个更好的说法是“该项目如何将其学生的一个project从“无”更改为projname一旦你这样说,答案几乎是显而易见的:

// in Project:
mstore[id].setSProject(projname);

请注意,Student还没有setSProject(string),您必须添加它。另外,请注意,此解决方案并不理想,因为 1) 任何人都可以更改 Student's project,以及 2) Student'sproject实际上不必是真实项目的名称。处理这些问题的方法不止一种。

于 2012-08-31T03:57:44.550 回答