繁体   English   中英

如何在地图中拥有2个以上的数据C ++

[英]How to have more than 2 data in a map c++

目前,我在一个元组中大约有2个数据,一个是字符串,另一个是元组,我想将数据写入文件,当前看起来像这样

Name  Indian Math English History Moral AverageScore
Anu     100   100   100     100    100      100

但是我想要的是

StudentNo Gender Name  Indian Math English History Moral AverageScore
  0223      M     Anu     100   100   100     100    100      100

尝试添加StudentNo和Gender,但是问题是Map只能包含2个数据,如果我输入了1个以上的数据,IDE会吐出一个错误。 在while循环中检查代码。

这是代码

//  Function to modify a student's exam scores.
void Student::modifyScore(string newName, int newIndian, int newEnglish, int newMath, int newHistory, int newMoral) {

    // How to add more than 1 data?
    map<string, tuple<int, int, int, int, int> > data;

    // Read file and fill data map
    ifstream studentRec("StudentRecord.txt");
    string line;

    while (getline(studentRec, line))
    {
        string name;

       int studentNo;
        // char gender; Trying to add these two.
       // string name;
       int indian, english, math, history, moral;

       stringstream ss(line);
       ss >> name >> indian >> english >> math >> history >> moral;
       data[name] = make_tuple(indian, english, math, history, moral);

    }

    studentRec.close();

    // Modify data
    data[newName] = make_tuple(newIndian,newEnglish, newMath, newHistory, newMoral);
    // Open same file for output, overwrite existing data
    ofstream ofs("StudentRecord.txt");

    for (auto entry = data.begin(); entry != data.end(); ++entry)
    {
        tie(newIndian,newEnglish, newMath, newHistory, newMoral) = entry->second;
        int average = averageScore(newIndian,newEnglish, newMath, newHistory, newMoral);

        ofs << left <<  setw(15) << entry->first << setw(15) << newIndian << setprecision(2) << newEnglish << setw(15) << right << newMath << setw(15) << newHistory << setw(15) <<  newMoral << average << endl;
    }
    ofs.close();


}

该程序的作用是用新数据修改文件中的现有数据。 我只是不知道如何修改此功能以满足我的需要。

您必须更改地图(尤其是地图的键),在元组中添加其他字段。 请让我知道它是否有效。

//  Function to modify a student's exam scores.
    void Student::modifyScore(string newName, int newIndian, int newEnglish, int newMath, int newHistory, int newMoral) {


    map<int, tuple<char,std::string,int, int, int, int, int> > data;

    // Read file and fill data map
    ifstream studentRec("StudentRecord.txt");
    string line;

    while (getline(studentRec, line))
    {
       string name;
       int studentNo;
       char gender; Trying to add these two.

       int indian, english, math, history, moral;

       stringstream ss(line);
       ss >> studentNo>>gender>>name >> indian>> english >> math >> history >> moral;
       data[studentNo] = make_tuple(gender,name,indian, english, math, history, moral);

    }

    studentRec.close();

    // Modify data
    data[studentNo] = make_tuple(newGender,newName,newIndian,newEnglish, newMath, newHistory, newMoral);
    // Open same file for output, overwrite existing data
    ofstream ofs("StudentRecord.txt");

    for (auto entry = data.begin(); entry != data.end(); ++entry)
    {
        tie(newGender,newName,newIndian,newEnglish, newMath, newHistory, newMoral) = entry->second;
        int average = averageScore(newIndian,newEnglish, newMath, newHistory, newMoral);

        ofs << left <<  setw(15) << entry->first << setw(15) <<newGender<<newName<< newIndian << setprecision(2) << newEnglish << setw(15) << right << newMath << setw(15) << newHistory << setw(15) <<  newMoral << average << endl;
    }
    ofs.close();


}

据我了解,您想将StudentNoGenderName字段作为map unique键(顺便说一句,为什么不仅是StudentNo ?)。 因此,您需要将它们放入一种数据类型,如下所示:

struct StudentKey
{
    int id;
    char gender;
    std::string name;
};

并提供operator< ,因为您正在使用带有默认比较器的std::map 但是不要这样做! 如果您只想从文件中读取数据,对其进行修改并放回文件中,那么您只需要这样的内容:

struct Student
{
    int StudentNo;
    char Gender;
    std::string Name;
    int Indian;
    int Math;
    int English;
    int History;
    int Moral;
    int AverageScore;
};

    //  Function to modify a student's exam scores.
void Student::modifyScore(const Student& newStudent) {
    std::vector<Student> data;
    // Read file and fill data map
    ifstream studentRec("StudentRecord.txt");
    string line;

    while (getline(studentRec, line))
    {
        Student s{};
        stringstream ss(line);
        if(ss >> s.StudentNo >> s.Gender >> s.Name >> s.Indian >> s.Math >> s.English >> s.History >> s,Moral)
            data.push_back(s);
    }

    studentRec.close();

    // Modify data
    data.push_back(newStudent);
    // Open same file for output, overwrite existing data
    ofstream ofs("StudentRecord.txt");

    for(auto& s : data)
    {
        s.AverageScore = averageScore(s.Indian, ...);

        ofs << left <<  setw(15) << s.StudentNo << ... << average << endl;
    }
    ofs.close();

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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