C++ 如何在地图c++;

C++ 如何在地图c++;,c++,C++,目前,我在一个元组中大约有两个数据,一个是字符串,另一个是元组 我想将数据写入当前看起来像这样的文件 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 An

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

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


}
//修改学生考试成绩的函数。
void Student::modifyScore(字符串newName、int newIndian、int newEnglish、int newMath、int newHistory、int newMoral){
//如何添加多个数据?
地图数据;
//读取文件并填充数据映射
ifstream studentRec(“StudentRecord.txt”);
弦线;
while(getline(studentRec,line))
{
字符串名;
国际学生号;
//char性别;尝试添加这两个。
//字符串名;
印度语、英语、数学、历史、道德;
弦流ss(线);
ss>>姓名>>印度>>英语>>数学>>历史>>道德;
数据[名称]=组合(印度语、英语、数学、历史、道德);
}
studentRec.close();
//修改数据
数据[newName]=生成元组(newIndian、newEnglish、newMath、newHistory、newMoral);
//打开同一文件进行输出,覆盖现有数据
ofs流(“StudentRecord.txt”);
对于(自动输入=数据.begin();输入!=数据.end();++entry)
{
tie(新印度语、新英语、新数学、新历史、新道德)=入门->第二;
int average=平均分数(新印度语、新英语、新数学、新历史、新道德);

ofs据我所知,您希望将
StudentNo
Gender
Name
字段设置为
地图的
唯一的
键(顺便说一句,为什么不仅仅是
StudentNo
?)。因此您需要将它们放在一个数据类型中,如下所示:

struct StudentKey
{
    int id;
    char gender;
    std::string name;
};
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();
并提供
operator>s.StudentNo>>s.Gender>>s.Name>>s.Indian>>s.Math>>s.English>>s.History>>s.Moral)
数据。推回;
}
studentRec.close();
//修改数据
数据。推回(newStudent);
//打开同一文件进行输出,覆盖现有数据
ofs流(“StudentRecord.txt”);
用于(自动和s:数据)
{
s、 AverageScore=AverageScore(s.Indian,…);

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

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


}
//修改学生考试成绩的函数。
void Student::modifyScore(字符串newName、int newIndian、int newEnglish、int newMath、int newHistory、int newMoral){
地图数据;
//读取文件并填充数据映射
ifstream studentRec(“StudentRecord.txt”);
弦线;
while(getline(studentRec,line))
{
字符串名;
国际学生号;
char性别;尝试添加这两个。
印度语、英语、数学、历史、道德;
弦流ss(线);
学生号>>性别>>姓名>>印度>>英语>>数学>>历史>>道德;
数据[studentNo]=make_tuple(性别、姓名、印度人、英语、数学、历史、道德);
}
studentRec.close();
//修改数据
数据[studentNo]=make_tuple(新性别、新姓名、新印度语、新英语、新数学、新历史、新道德);
//打开同一文件进行输出,覆盖现有数据
ofs流(“StudentRecord.txt”);
对于(自动输入=数据.begin();输入!=数据.end();++entry)
{
tie(新性别、新姓名、新印度语、新英语、新数学、新历史、新道德)=输入->秒;
int average=平均分数(新印度语、新英语、新数学、新历史、新道德);

ofs不理解您的问题。您认为“可以有2个数据”是什么意思?
map
是一个带有{
}的数组成对。
key
可以是比
std::string
更复杂的数据类型。是否要将
StudentNo
Name
Gender
作为映射中的
键?是的,但我无法将StudentNo和Gender作为键,因为我的数据类型不能超过2个。例如,我不能有map数据为什么要将性别作为键?请阅读有关映射的内容,了解此上下文中的键和值的含义。您只需要一个复合值,例如:一个对象。@KarolyHorvath,因为我需要用性别和StudentNo数据覆盖文件。该键用于查找。如果不按性别查找,则它不是键。与此相反他说,他显然不想要复合键。嗯。这就是我说“不要这样做”的原因。我只是写了
StudentKey
作为复合键的一个例子。但无论如何,这并不重要,谢谢