1

I'm trying to insert the contents of one map to another. Here is the code:

std::map<std::string, int> Student::getGrades() const;
...

for(set<Student*, Cmp>::const_iterator it = s.begin(); it!=s.end(); ++it)
{
    grades.clear();
    grades.insert((*it)->getGrades().begin(), (*it)->getGrades().end());
    for(map<string, int>::const_iterator itt = grades.begin(); itt!=grades.end(); ++itt)
    {
       if(itt->first == course && itt->second >= score1 && itt->second <= score2)
            (*it)->display(cout);
    }
}

s is a set that contains pointers to student objects and each student objects has a getGrades() method that returns a map. I'm trying to find grades that match grades that I have read in from a file and print the record corresponding to those grades. However, the insert method is giving me a seg fault. Any suggestions?

4

2 に答える 2

3

ケースでは、参照でgetGrades()はなくグレード マップのコピーを返し、2 つの異なるマップに属します。begin()end()

おそらくローカル コピーを作成し、次のようにコードを簡略化する必要があります。

for(set<Student*, Cmp>::const_iterator it = s.begin(); it!=s.end(); ++it)
{
    map<string, int> grades = (*it)->getGrades();
    for(map<string, int>::const_iterator itt = grades.begin(); itt!=grades.end(); ++itt)
    {
        if(itt->first == course && itt->second >= score1 && itt->second <= score2)
            (*it)->display(cout);
    }
}
于 2012-04-09T03:23:20.527 に答える
0

セット内に初期化されていないポインタがある可能性がありますs

于 2012-04-09T03:24:12.557 に答える