I’m currently working on a DNA database class and I currently associate each row in the database with both a match score (based on edit distance) and the actual DNA sequence itself, is it safe to modify first this way within an iteration loop?
typedef std::pair<int, DnaDatabaseRow> DnaPairT; typedef std::vector<DnaPairT> DnaDatabaseT; // .... for(DnaDatabaseT::iterator it = database.begin(); it != database.end(); it++) { int score = it->second.query(query); it->first = score; }
The reason I am doing this is so that I can sort them by score later. I have tried maps and received a compilation error about modifying first, but is there perhaps a better way than this to store all the information for sorting later?
To answer your first question, yes. It is perfectly safe to modify the members of your pair, since the actual data in the pair does not affect the vector itself.
edit: I have a feeling that you were getting an error when using a map because you tried to modify the
firstvalue of the map’s internal pair. That would not be allowed because that value is part of the map’s inner workings.As stated by dribeas:
edit: To answer your second question, I see nothing at all wrong with the way you are structuring the data, but I would have the database hold pointers to
DnaPairTobjects, instead of the objects themselves. This would dramatically reduce the amount of memory that gets copied around during the sort procedure.The only reason you might need to look into more efficient methods is if your database is so enormous that the sorting loop takes too long to complete. If that is the case, though, I would imagine that your
queryfunction would be the one taking up most of the processing time.