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

时间:2015-06-20 15:49:05

标签: 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();


}

该程序的作用是使用新数据修改文件中的现有数据。  我根本无法弄清楚如何修改这个功能以满足我的需要。

2 个答案:

答案 0 :(得分:0)

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

//  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();


}

答案 1 :(得分:-1)

据我了解,您希望将StudentNoGenderName字段设为unique的{​​{1}}个字段(顺便说一句,为什么不仅仅{ {1}}?)。所以你需要将它们放在一个数据类型中,如下所示:

map

并提供StudentNo,因为您使用的是struct StudentKey { int id; char gender; std::string name; }; 默认比较器。但是不要这样做! 如果您只想从文件中读取数据,修改它并将其放回文件中,那么您只需要这样的内容:

operator<

}