views:

378

answers:

2

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?

+5  A: 
e.James
In maps you cannot change first as it would break the invariant of the map being a sorted balanced tree.
David Rodríguez - dribeas
@dribeas: Perfect description. thank you! I have added that line to my answer.
e.James
A: 

You can't modify since the variable first of std::pair is defined const

jab